Best practice parsing JSON?

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
Grovkillen
Automation Wizard
Posts: 349
Joined: Fri Aug 10, 2012 2:38 pm
Location: Hudiksvall, Sweden
Contact:

Best practice parsing JSON?

Post by Grovkillen » Mon Jan 14, 2019 6:45 pm

If I fetch a supposed JSON string the JSON parser will throw an error if it's not a correct JSON format.

Could it be possible to have an option to return a error string instead or should I do a JSON validation myself before throwing an JSON to the parser?
Let>ME=%Script%

Running: MS14.5.4

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

Re: Best practice parsing JSON?

Post by Marcus Tettmar » Tue Jan 15, 2019 9:45 am

Use IGNOREERRORS and/or ONERROR and look at the LAST_ERROR variable to see what the error is, if any.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

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

User avatar
Grovkillen
Automation Wizard
Posts: 349
Joined: Fri Aug 10, 2012 2:38 pm
Location: Hudiksvall, Sweden
Contact:

Re: Best practice parsing JSON?

Post by Grovkillen » Tue Jan 15, 2019 12:51 pm

Thanks Marcus! Worked like a charm! :D

If someone else is wondering:

Code: Select all

Let>ONERROR=CHECK_ERROR

Let>TEST_URL=https://jsonplaceholder.typicode.com/users
HTTPRequest>TEST_URL,,GET,,TEST_JSON,,,,
JSONParse>TEST_JSON,$[*]['id'],TEST_ID
MDL>TEST_ID_1

Let>TEST_URL=error
HTTPRequest>TEST_URL,,GET,,TEST_JSON,,,,
JSONParse>TEST_JSON,$[*]['id'],TEST_ID
MDL>TEST_ID_1

SRT>CHECK_ERROR
  //MDL>LAST_ERROR
  Let>TEST_ID_1=LAST_ERROR
END>CHECK_ERROR
Let>ME=%Script%

Running: MS14.5.4

Post Reply