Test that JSON received is valid

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
KimD
Posts: 223
Joined: Wed Jul 08, 2015 5:51 am
Location: Wellington, New Zealand

Test that JSON received is valid

Post by KimD » Fri May 18, 2018 5:15 am

I'm working on an app that reads JSON from a web service. For this I'm using JSONImport, putting the JSON into a LC array and from the array into an SQLite db. It seems to work nicely. Then I thought "I really should code this so that it doesn't explode if the web service passes it invalid JSON". So to see what would happen if the app received invalid JSON I:
- wrote the working JSON that I received from the web service into a file; then
- chopped the final "}" off the JSON (to make it incorrect); then
- had JSONImport read the file containing the incorrect JSON.

The IDE stops dead with an "LCB Error in file json.lcb at line 65: syntax error: 1:2734 unexpected end of input" error.

Is there an easy way for me to test that JSON is valid before I pass it to JSONImport?

Ultimately my app will need to run on Windows desktop + Android + IOS. I've seen a few forum posts about an IsJSON function in a fastJSON library. Is that the way to go? I was hoping to be able to keep using JSONImport as it seems to do the job fine when it does receive valid JSON.

Thanks in advance

Kim

LC9 on Windows 10

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Test that JSON received is valid

Post by dunbarx » Fri May 18, 2018 1:37 pm

Not specific to your import, would using the "try/catch" structure do the trick?

Craig

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: Test that JSON received is valid

Post by MaxV » Fri May 18, 2018 3:00 pm

Just use regex, this is the code in Javascript:

Code: Select all

 var my_JSON_object = !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
             text.replace(/"(\\.|[^"\\])*"/g, ''))) &&
         eval('(' + text + ')');
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Test that JSON received is valid

Post by jacque » Fri May 18, 2018 4:21 pm

I've used try!/catch around a JSONImport function and it works. No need for regex that way.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

KimD
Posts: 223
Joined: Wed Jul 08, 2015 5:51 am
Location: Wellington, New Zealand

Re: Test that JSON received is valid

Post by KimD » Fri May 18, 2018 9:40 pm

Thanks Craig, Max & Jacque. I will give try / catch a go. So much to learn............

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”