mojomatt

Json Parsing Question

4 posts in this topic

Hi All,  

Can anyone help me figure out the syntax to use to get the jobid returned out of the below json string (in my code example)?  I want to use bracketed notation and I want the msgbox to display 2155813bb95e9829.

One example I've tried that doesn't work is...

$strJobIDFromJson = json_Get($objJson, "[jobid]")

 

 

I'm using the json.au3 include with these properties...

; File        : Json.au3 (2015.01.08)
; Purpose    : A Non-Strict JavaScript Object Notation (JSON) Parser UDF
; Author    : Ward
; Dependency: BinaryCall.au3
; Website    : http://www.json.org/index.html
;
; Source    : jsmn.c
; Author    : zserge
; Website    : http://zserge.com/jsmn.html
;
; Source    : json_string_encode.c, json_string_decode.c
; Author    : Ward

 

Here's my example code...

#Include <JSON.au3>
$strReturnedData2 = '{"jobid":{"2155813bb95e9829":{"startTime":"2016-10-28 20:56:53 UTC","requestor":"Requestor","description":"Description","email":"","content":[{"Name":"ContentName","Parameters":""}],"computers":{"Computername":{"overallStatus":"Waiting for client to check-in...","details":[{"name":"DetailsName","env":"","actionID":"","status":"","exitCode":"","lastLine":"","startTime":"","endTime":"","contentURL":""}]}},"actionStatus":{"iem-dev":"Open"}}},"status_text":"","error_text":"","status_code":"200","status":"OK","rc":"0"}'
$objJson = json_Decode($strReturnedData2)
$strJobIDFromJson = json_Get($objJson, What Do I Put Here?)
MsgBox(0,"JobID",$strJobIDFromJson)

 

Thanks

 

 

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hello. You can do this.

#include <JSON.au3>
Local $strReturnedData2 = '{"jobid":{"2155813bb95e9829":{"startTime":"2016-10-28 20:56:53 UTC","requestor":"Requestor","description":"Description","email":"","content":[{"Name":"ContentName","Parameters":""}],"computers":{"Computername":{"overallStatus":"Waiting for client to check-in...","details":[{"name":"DetailsName","env":"","actionID":"","status":"","exitCode":"","lastLine":"","startTime":"","endTime":"","contentURL":""}]}},"actionStatus":{"iem-dev":"Open"}}},"status_text":"","error_text":"","status_code":"200","status":"OK","rc":"0"}'
Local $objJson = json_Decode($strReturnedData2)
Local $strJobIDFromJson = json_Get($objJson, '["jobid"]').Keys[0]
ConsoleWrite($strJobIDFromJson & @CRLF)

 

Saludos

Edited by Danyfirex

Share this post


Link to post
Share on other sites

Alternatively you could use a regular expression

$strReturnedData2 = '{"jobid":{"2155813bb95e9829":{"startTime":"2016-10-28 20:56:53 UTC","requestor":"Requestor","description":"Description","email":"","content":[{"Name":"ContentName","Parameters":""}],"computers":{"Computername":{"overallStatus":"Waiting for client to check-in...","details":[{"name":"DetailsName","env":"","actionID":"","status":"","exitCode":"","lastLine":"","startTime":"","endTime":"","contentURL":""}]}},"actionStatus":{"iem-dev":"Open"}}},"status_text":"","error_text":"","status_code":"200","status":"OK","rc":"0"}'

$jobid = StringRegExpReplace($strReturnedData2, '.*"jobid":\{"([^"]+).*', "$1")
MsgBox(0, "JobID", $jobid)

 

Share this post


Link to post
Share on other sites

Thanks Danyfirex that syntax works.  :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • satanico64
      By satanico64
      hi guys !
      how are you ? fine ? yeahhh
      Here is my problem:
      I have 2 autoit applications, a.exe and b.exe
      In a.exe, i call b.exe with a json as parameter:
      FileWrite("N:\+++ Dev\ClientServeur\FolderMonitor.nca", "lejson in A:" & Json_Encode($json_tmp)& @CRLF) Run(@ScriptDir & "\FolderMonitor.exe " & Json_Encode($json_tmp) )                        
              In program B:
      ;Firstline of program: FileWrite("N:\+++ Dev\ClientServeur\FolderMonitor.nca", @CRLF & "json in B:" & $CmdLine[1] & @CRLF)        
              Output:
                     
              lejson in A:{"DOSSIERS":{"Dossier_Hidden":"N:\\+++ Dev\\ClientServeur\\","Dossier_du_script":"N:\\+++ Dev\\ClientServeur\\","Fichier_Historique":"N:\\+++ Dev\\ClientServeur\\Historique.log","Fichier_DEBUG":"N:\\+++ Dev\\ClientServeur\\FolderMonitor.nca"}}
              json in B  :{DOSSIERS:{Dossier_Hidden:N:\\+++ Dev\\ClientServeur\\,Dossier_du_script:N:\\+++ Dev\\ClientServeur\\,Fichier_Historique:N:\\+++ Dev\\ClientServeur\\Historique.log,Fichier_DEBUG:N:\\+++ Dev\\ClientServeur\\FolderMonitor.nca}}
      As you see, i lost my quote in my json so it's au problem to parse my json.
      I use THIS library
      I tried different type of encoding (ex JSON_STRICT_PRINT), but i don't think that it's the problem. Maybe i'm wrong
       
      thanks
      Nicolas.
    • jandings
      By jandings
      Hello there,
      since I spent some time to access the REST-API of the web shop system Shopware, I'd like to share a few lines to make life easier for others.
      With this you can access your Shopware database, reading and writing all kind of data. 
      Replies are JSON style.
      To work with JSON I usually depend on either just own AutoIt string operations or this AutoIt library:
      ; File        : Json.au3 (2015.01.08)
      ; Purpose    : A Non-Strict JavaScript Object Notation (JSON) Parser UDF
      ; Author    : Ward
      $UserName="xxxx" ;Shopware credentials of Shopware user who has the API checkbox ticked $PassWord="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ;API-Key of this user, NOT the Password $URL="www.XXX.com/api/" $Command="orders/64682?useNumberAsId=true" ;A list of commands and options is available through shopware REST-API help; Here we read order number 64682 $oHTTP = ObjCreate("winhttp.winhttprequest.5.1") $oHTTP.Open("GET", "https://" & $URL & $Command , False) $oHTTP.SetCredentials($UserName, $PassWord, 0) $oHTTP.Send() $response = $oHTTP.ResponseText ConsoleWrite ($response & @CRLF & @CRLF)  
    • BJP
      By BJP
      Hello.  I have a program that has used ADO database connection to return a query and then subsequently put the query results into an array using getrows.  See snippet below:
      $constrim="DRIVER={SQL Server};SERVER=xxx-xxxxx\CSC;DATABASE=xxxxxxxxx;uid=xxxxxxxxxx;pwd=xxxxxxxxxxxx;" $adCN = ObjCreate ("ADODB.Connection") ; <== Create SQL connection $adCN.Open ($constrim) ; local $sQuery = "select * from tbl_Apps"                     ; get all applications in the database local $oAppRecordSet = $adCN.Execute($sQuery) local $aAppsInDB = $oAppRecordSet.Getrows(5000) With the code above I can perform array operations very efficiently. 
      Now I need to get this data via JSON which is working (using Ward's JSON UDF)  but the data set returned is large (approx 4MB) and I'm wondering what is the most efficient way to get this data into an array.  
      Dim $obj = ObjCreate ("WinHttp.WinHttpRequest.5.1") $obj.Open("GET", $URL, false) $obj.SetRequestHeader("Content-Type", "application/json") $obj.Send() $json = JSON_decode( $obj.ResponseText ) Any help would be appreciated!!
    • baolo073
      By baolo073
      [ [ [ "Kinh Oanh,\r\n", "Dear Oanh,\r\n", null, null, 3 ], [ "C\u1ea3m \u01a1n b\u1ea1n r\u1ea5t nhi\u1ec1u v졢\u1ee9c th\u01b0 c\u1ee7a b\u1ea1n \u0111\u1ebfn m\u1ed9t v᩠ngṠtr\u01b0\u1edbc. ", "Thank you very much for your letter which arrived a few days ago.", null, null, 3 ], [ "Th\u1eadt \u0111⯧ y뵠khi nghe t\u1eeb b\u1ea1n.\r\n", "It was lovely to hear from you.\r\n", null, null, 3 ], [ "b\u1ea1n \u0111i \u0111㵠v\u1eady.\r\n", "where do you go.\r\n", null, null, 1 ], [ "T\u1ea1m bi\u1ec7t!", "Goodbye!", null, null, 1 ] ], null, "en", null, null, null, 0.91366601, null, [ [ "en" ], null, [ 0.91366601 ], [ "en" ] ] ] How to parse array to json?
    • NiftRex
      By NiftRex
      I'm trying to get an array from a website so that I can just get the url, but I am not sure how. I read a bit of arrays but I have a feeling I'd have to be writing a lot more than what I should be. I will include the script I have so far and the API url for what I want.
       
      API: https://api.fast.com/netflix/speedtest?https=true&token=YXNkZmFzZGxmbnNkYWZoYXNkZmhrYWxm&urlCount=1 (I want the 'url' array that contains the url)
       
      Code:
      #include <MsgBoxConstants.au3> #include <Inet.au3> #include <Array.au3> $site = _INetGetSource('http://api.fast.com/netflix/speedtest?https=true&token=YXNkZmFzZGxmbnNkYWZoYXNkZmhrYWxm&urlCount=1') MsgBox($MB_SYSTEMMODAL, "Title", $site[1])