Verify JSON Data
Denis McMahon
denismfmcmahon at gmail.com
Mon May 26 11:43:30 EDT 2014
On Mon, 26 May 2014 07:26:20 -0700, gaurangnshah wrote:
> Is there any module through which i can verify JSON file like DOM or
> Object oriented way. ( i.e. data.key)
Where is the json data coming from? What do you mean by verify?
https://docs.python.org/2/library/json.html#encoders-and-decoders
explains how json object strings get decoded to python data types. A json
object string should at the highest level be either an object or an
array, although the python decoder can also handle strings, numbers and a
few special values.
Are you trying to check that the json string is valid json code (ie json
lint) or are you trying to check that it meets some specific structure,
in which case the only way to verify it is to decode it and check the
structure.
Note that not all valid python structures can be successfully converted
to json objects, for example a python dictionary can have tuples as keys,
but a json object can not have an array as an attribute name. For example:
d = { (1,2,3):'one',('a','b','c'):'two' }
print d
print json.JSONEncoder().encode( d )
Gives a TypeError in the json code "keys must be a string"
If you have a debian based linux distro, you can get jsonlint with:
sudo apt-get install python-demjson
which provides a command line json syntax checker and formatter.
Otherwise, google json lint, there are several web based tools that seem
to be able to do something similar.
--
Denis McMahon, denismfmcmahon at gmail.com
More information about the Python-list
mailing list