JSONParse - JSONPath help

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
tbrahmer
Newbie
Posts: 15
Joined: Thu May 06, 2010 10:14 pm

JSONParse - JSONPath help

Post by tbrahmer » Thu Feb 09, 2017 3:33 pm

I am receiving a json response formatted exactly in this manner:

Code: Select all

[{"usr_uid":"000","usr_name":"admin"},{"usr_uid":"127","usr_name":"bot"},{"usr_uid":"269","usr_name":"fred"}]
I can't seem to find the proper way to reference the nodes in this structure via the JSONParse command. I've checked the online validators and they say it is valid JSON and I am able to reference nodes properly using those online validators. For instance $[0].usr_name returns "admin", $[1].usr_uid returns "127", etc. The '$' character is supposed to represent the 'root' of the structure.

However, I am unable to get the JSONParse command to properly reference the array elements when they are directly at the root level like this. Using similar notation in Macro Scheduler ( $[X].objname ) isn't returning any match. Any ideas?

Thank you very much for any help you can give me!

User avatar
Marcus Tettmar
Site Admin
Posts: 7393
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: JSONParse - JSONPath help

Post by Marcus Tettmar » Fri Feb 10, 2017 7:39 am

That's a JSON array but not a JSON object. It's only part of a JSON object. The array has no name.

If you give it a name we can reference it:

Code: Select all

/*
JSON:
{"things":[{"usr_uid":"000","usr_name":"admin"},{"usr_uid":"127","usr_name":"bot"},{"usr_uid":"269","usr_name":"fred"}]}
*/
LabelToVar>JSON,sJSON
JSONParse>sJSON,things[1].usr_uid,res
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

tbrahmer
Newbie
Posts: 15
Joined: Thu May 06, 2010 10:14 pm

Re: JSONParse - JSONPath help

Post by tbrahmer » Fri Feb 10, 2017 2:52 pm

Thank you Marcus, I see that will work. I will add code to alter that response after I get it and give the array a name like you demonstrated. Thanks for your help!

pfischer
Junior Coder
Posts: 21
Joined: Wed Jul 23, 2014 2:07 pm

Re: JSONParse - JSONPath help

Post by pfischer » Tue May 29, 2018 8:36 pm

Dear Marcus,
I just found this post from some years ago with respet to a json output. However, I really do not get it.

My JSON output is exactly like the output from tbrahmer:

[{"usr_uid":"000","usr_name":"admin"},{"usr_uid":"127","usr_name":"bot"},{"usr_uid":"269","usr_name":"fred"}]

In your reply you recommend to give the array a name and in you give the following code as solution:
/*
JSON:
{"things":[{"usr_uid":"000","usr_name":"admin"},{"usr_uid":"127","usr_name":"bot"},{"usr_uid":"269","usr_name":"fred"}]}
*/
LabelToVar>JSON,sJSON
JSONParse>sJSON,things[1].usr_uid,res

However, the upper lines are commented -> so when I have the string [{"usr_uid":"000","usr_name":"admin"},{"usr_uid":"127","usr_name":"bot"},{"usr_uid":"269","usr_name":"fred"}], how do you give this array a name? as you recommend. It cannot be by the commented code section because this is inactive??

Probably I hav a block in my mind but can you explain how to give the string a name?

Thanks
Phil

User avatar
Grovkillen
Automation Wizard
Posts: 1128
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: JSONParse - JSONPath help

Post by Grovkillen » Wed May 30, 2018 5:22 am

LabelToVar takes the commented lines and put them as a variable. So it's perfectly fine that they are commented since you don't want the compiler to act on that part of the code.
Let>ME=%Script%

Running: 15.0.27
version history

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts