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
Example
// JSONParse example using filter predicates (?[...]) and @ (current node)
// Requires the extended Neslib JSONPath (filter support).
// Sample JSON: a store with books and a bicycle
LabelToVar>myJSON,json
// ---- Example 1: Books cheaper than 10 (filter: @.price < 10) ----
// Path: $.store.book[[email protected] < 10] --> matches each book object where price < 10
JSONParse>json,$.store.book[[email protected] < 10],cheapBooks
MessageModal>Found %cheapBooks_count% book(s) under 10. First: %cheapBooks_1%
// ---- Example 2: Books that have an ISBN (existence filter: [email protected]) ----
JSONParse>json,$.store.book[[email protected]],withIsbn
MessageModal>Books with ISBN: %withIsbn_count%. First title: %withIsbn_1%
// ---- Example 3: Books by a specific author (filter: @.author == "Tolkien") ----
JSONParse>json,$.store.book[[email protected] == "J. R. R. Tolkien"],tolkien
MessageModal>Tolkien book: %tolkien_1%
// ---- Example 4: Titles of books under 15 (filter then .title) ----
JSONParse>json,$.store.book[[email protected] < 15].title,titlesUnder15
MessageModal>Titles under 15: %titlesUnder15_count% - e.g. %titlesUnder15_1%
// ---- Example 5: Logical OR - cheap OR has ISBN ----
JSONParse>json,$.store.book[[email protected] < 10 || @.isbn],cheapOrIsbn
MessageModal>Cheap or with ISBN: %cheapOrIsbn_count% items
// ---- Example 6: Negation - books without ISBN ----
JSONParse>json,$.store.book[[email protected]],noIsbn
MessageModal>Books without ISBN: %noIsbn_count%
/*
myJSON:
{"store":{"book":[
{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},
{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},
{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},
{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}
],"bicycle":{"color":"red","price":399}}}
*/