Verify JSON Data

Gene Heskett gheskett at shentel.net
Mon May 26 11:37:10 EDT 2014


On Monday 26 May 2014 11:19:53 Roy Smith did opine
And Gene did reply:
> In article <e26d3f14-ac97-4abd-bdfc-699d9ed2175c at googlegroups.com>,
> 
>  gaurangnshah at gmail.com wrote:
> > Hi Guys,
> > 
> > Would someone let me know how to verify JSON data in python. There
> > are so many modules available to verify XML file, however i didn't
> > find any good module to verify JSON Data.
> 
> Python comes with a built-in json module.  Just use json.load() or
> json.loads() to parse your JSON data.  The first call reads from a
> string, the second on from a file, but in all other ways, they're
> identical.
> 
> There are a bunch of third-party modules (ujson, etc) which are faster,
> but fundamentally, they're all the same.
> 
> If I understand you correctly, you're reading a JSON document which is
> so large that if you store the converted data as a Python object, you
> run out of memory?  If that's the case, I'm not sure if there's a good
> pure Python solution.  I don't know of any json modules which parse,
> but don't store, the data.
> 
> Depending on what operating system you're on, there may be a
> command-line utility which parse JSON.  For example, on Ubuntu linux,
> there's "json_xs".  Perhaps shell out to that, use the "-t null" output
> format, redirect the output to /dev/null, and see what exit status you
> get:
> 
> # Good JSON
> $ echo '[1, 2, 3]' | json_xs -t null 2>/dev/null; echo $?
> 0
> 
> # Bad JSON
> $ echo '[1; 2, 3]' | json_xs -t null 2>/dev/null; echo $?
> 255
> 
> Wrap this up in a subprocess.check_output() call, and you're done.

Just for S&G, and without checking the version numbers of anything, this 
may not be all that bulletproof a test:

gene at coyote:~$ echo '[1, 2, 3]' | json_xs -t null 2>/dev/null; echo $?
127
gene at coyote:~$ echo '[1; 2, 3]' | json_xs -t null 2>/dev/null; echo $?
127

Old, buntu 10.04.4 LTS system, all up to date security patches wise.  
kernal 3.13.9, PAE on a quad core phenom.

Interesting result.  Source of error? DamnedifIknow.

Cheers, Gene Heskett
-- 
"There are four boxes to be used in defense of liberty:
 soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
Genes Web page <http://geneslinuxbox.net:6309/gene>
US V Castleman, SCOTUS, Mar 2014 is grounds for Impeaching SCOTUS



More information about the Python-list mailing list