Macro Scheduler 15

JSONParse

JSONParse>json_string,json_path,result

 

Uses JSONPath syntax to extract data from a JSON string. Takes a valid json string, a json path string and returns an array in result. result_count will contain the number of matches.

 

For details on the syntax supported see:

http://goessner.net/articles/JsonPath/

 

Breaking change: Please note that this function changed in version 14.5, returning an array instead of a simple variable and requiring the '$' root operator. For backwards compatibility set JSONPARSE_LEGACY to 1. Old scripts will return an error without either setting JSONPARSE_LEGACY to 1 or updating the syntax by adding $. to the front of the path and referring to result_1 instead of result. See examples below.

 

Example

 

HTTPRequest>http://ip.jsontest.com/,,GET,,JSON

JSONParse>JSON,$.ip,myIP

If>myIP_count=1

 MessageModal>Your pubic IP is: %myIP_1%

Else

 MessageModal>Failed to get IP

Endif

 

Example

 

/*

MyJSON:

{ "uid" : "1234", 

  "clients" : ["client1","client2","client3"],

  "people" : [{"Name":"Marcus","Age":"21"},{"Name":"Dorian","Age":"18"}],

  "color" : "red",

  "size" : 14 }

*/

 

LabelToVar>MyJSON,sJSON

 

//get the UID

JSONParse>sJSON,$.uid,result

MessageModal>UID is: %result_1%

 

//gets the first client

JSONParse>sJSON,$.clients[0],result

MessageModal>client 1 is %result_1%

 

//gets the second name (base zero)

JSONParse>sJSON,$.people[1].Name,result

MessageModal>second name is %result_1%

 

//this will get an array of all the names

JSONParse>sJSON,$.people[*].Name,result

 

//let's loop through the output 

Let>k=0

Repeat>k

  Let>k=k+1

  Let>this_name=result_%k%

  MessageModal>this_name

Until>k=result_count

 

//use the [' ... '] format instead of . - this is useful if names have dots or special chars in them

JSONParse>sJSON,$['people'][1]['Name'],result