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

    • 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])  
    • 31290
      By 31290
      Hi Guys, 
      Since I'm able to get a Dell equipment warranty status thanks to my API key, I'm using an UDF to extract data from an XML file and get the end date. > 
      Thing is, when using InetGet, the original file is in JSON format and the UDF is not working anymore, even if I download the file with the xml extension. Therefore, and when I manually download the page with Chrome, I have a proper XML file where the UDF is working fine.
      Here's my code:
      I even tried to convert the json to xml > https://www.autoitscript.com/forum/topic/185717-js-json-to-xml/
      I took a look here https://www.autoitscript.com/forum/topic/104150-json-udf-library-fully-rfc4627-compliant/ but I don't understand anything :/
       
      The XML read UDF is just perfect for my needs but I'm stuck here... 
      Thanks for any help you can provide
      -31290-
      3MTXM12.json
      3MTXM12.xml
    • cookiemonster
      By cookiemonster
      Im trying to read a JSON file into an array.
       
      The JSON looks like this:
       
      { "project_info": { "project_number": "123456789", "url": "https://www.website.com", "project_id": "PRJ08", "Bucket": "Buk09" }, "client": [ { "client_info": { "id": "1:1001306455", "info": { "name": "banana" } }, "oauth_client": [ { "client_id": "1001306455694-m3h6v", "client_type": 3 } ], "a_key": [ { "key": "dkldkdkd" } ], "Avail": { "ana": { "status": 1 }, "vit": { "status": 1, "other": [] }, "ad": { "status": 2 } } }, { "client_info": { "id": "1:1838346", "info": { "name": "orange" } }, "oauth_client": [ { "client_id": "2145696315633-dmdhe", "client_type": 3 } ], "a_key": [ { "key": "osikdme" } ], "Avail": { "ana": { "status": 1 }, "vit": { "status": 1, "other": [] }, "ad": { "status": 2 } } }, ], "configuration_version": "1" } What I want to do is read it all into one single large array, can anyone help? 
      All I have so far is:
      #include "JSON.au3" #include "array.au3" $file = fileread("C:\file.json") JsonToArray($file) Func JsonToArray($JSON) $JSON = StringRegExpReplace($JSON, "[\[\]{}]", "") $sBreak = StringSplit($JSON, ",") For $a = 1 To $sBreak[0] $t = _JSONDecode("{" & $sBreak[$a] & "}") _ArrayDisplay($t, "multi " & $a & " of " & $sBreak[0]) Next EndFunc ;==>JsonToArray  
    • ur
      By ur
      I have a github repository and when I run the below command it will retrieve a json file.
      curl --user "user:password" https://api.github.com/repos/ukreddy-erwin/Build-war/commits > Desktop/uday.html The output json file will be of the below format.
      [ { "sha": "525ddee5f25628f89c04980d7f683e9cd8f56b7a", "commit": { "author": { "name": "ukreddy", "email": "ukreddy@erwin.com", "date": "2016-12-26T12:27:14Z" }, "committer": { "name": "ukreddy", "email": "ukreddy@erwin.com", "date": "2016-12-26T12:27:14Z" }, "message": "changed gwt and gxt folder structure to build.xml", "tree": { "sha": "8d4f2cbca93efec18be8a17f25f78ad168e65347", "url": "https://api.github.com/repos/ukreddy-erwin/Build-war/git/trees/8d4f2cbca93efec18be8a17f25f78ad168e65347" }, "url": "https://api.github.com/repos/ukreddy-erwin/Build-war/git/commits/525ddee5f25628f89c04980d7f683e9cd8f56b7a", "comment_count": 0 }, "url": "https://api.github.com/repos/ukreddy-erwin/Build-war/commits/525ddee5f25628f89c04980d7f683e9cd8f56b7a", "html_url": "https://github.com/ukreddy-erwin/Build-war/commit/525ddee5f25628f89c04980d7f683e9cd8f56b7a", "comments_url": "https://api.github.com/repos/ukreddy-erwin/Build-war/commits/525ddee5f25628f89c04980d7f683e9cd8f56b7a/comments", "author": { "login": "ukreddy-erwin", "id": 21240709, "avatar_url": "https://avatars.githubusercontent.com/u/21240709?v=3", "gravatar_id": "", "url": "https://api.github.com/users/ukreddy-erwin", "html_url": "https://github.com/ukreddy-erwin", "followers_url": "https://api.github.com/users/ukreddy-erwin/followers", "following_url": "https://api.github.com/users/ukreddy-erwin/following{/other_user}", "gists_url": "https://api.github.com/users/ukreddy-erwin/gists{/gist_id}", "starred_url": "https://api.github.com/users/ukreddy-erwin/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/ukreddy-erwin/subscriptions", "organizations_url": "https://api.github.com/users/ukreddy-erwin/orgs", "repos_url": "https://api.github.com/users/ukreddy-erwin/repos", "events_url": "https://api.github.com/users/ukreddy-erwin/events{/privacy}", "received_events_url": "https://api.github.com/users/ukreddy-erwin/received_events", "type": "User", "site_admin": false }, "committer": { "login": "ukreddy-erwin", "id": 21240709, "avatar_url": "https://avatars.githubusercontent.com/u/21240709?v=3", "gravatar_id": "", "url": "https://api.github.com/users/ukreddy-erwin", "html_url": "https://github.com/ukreddy-erwin", "followers_url": "https://api.github.com/users/ukreddy-erwin/followers", "following_url": "https://api.github.com/users/ukreddy-erwin/following{/other_user}", "gists_url": "https://api.github.com/users/ukreddy-erwin/gists{/gist_id}", "starred_url": "https://api.github.com/users/ukreddy-erwin/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/ukreddy-erwin/subscriptions", "organizations_url": "https://api.github.com/users/ukreddy-erwin/orgs", "repos_url": "https://api.github.com/users/ukreddy-erwin/repos", "events_url": "https://api.github.com/users/ukreddy-erwin/events{/privacy}", "received_events_url": "https://api.github.com/users/ukreddy-erwin/received_events", "type": "User", "site_admin": false }, "parents": [ { "sha": "db4d61c5bbef555e6cc9f2e852aaf9d75772475c", "url": "https://api.github.com/repos/ukreddy-erwin/Build-war/commits/db4d61c5bbef555e6cc9f2e852aaf9d75772475c", "html_url": "https://github.com/ukreddy-erwin/Build-war/commit/db4d61c5bbef555e6cc9f2e852aaf9d75772475c" } ] } ] So, I need to parse above file and get the number of opening and closing brackets {} .
      So that would be the number of commits in the repository.
      Any suggestion on how to parse this file of json format.