How do I check if a string is valid JSON in Python? How do I check if a string is valid JSON in Python? json json

How do I check if a string is valid JSON in Python?

You can try to do json.loads(), which will throw a ValueError if the string you pass can't be decoded as JSON.

In general, the "Pythonic" philosophy for this kind of situation is called EAFP, for Easier to Ask for Forgiveness than Permission.

Example Python script returns a boolean if a string is valid json:

import jsondef is_json(myjson):  try:    json_object = json.loads(myjson)  except ValueError as e:    return False  return True

Which prints:

print is_json("{}")                          #prints Trueprint is_json("{asdf}")                      #prints Falseprint is_json('{ "age":100}')                #prints Trueprint is_json("{'age':100 }")                #prints Falseprint is_json("{\"age\":100 }")              #prints Trueprint is_json('{"age":100 }')                #prints Trueprint is_json('{"foo":[5,6.8],"foo":"bar"}') #prints True

Convert a JSON string to a Python dictionary:

import jsonmydict = json.loads('{"foo":"bar"}')print(mydict['foo'])    #prints barmylist = json.loads("[5,6,7]")print(mylist)[5, 6, 7]

Convert a python object to JSON string:

foo = {}foo['gummy'] = 'bear'print(json.dumps(foo))           #prints {"gummy": "bear"}

If you want access to low-level parsing, don't roll your own, use an existing library:

Great tutorial on python JSON module:

Is String JSON and show syntax errors and error messages:

sudo cpan JSON::XSecho '{"foo":[5,6.8],"foo":"bar" bar}' > myjson.jsonjson_xs -t none < myjson.json


, or } expected while parsing object/hash, at character offset 28 (before "bar}at /usr/local/bin/json_xs line 183, <STDIN> line 1.

json_xs is capable of syntax checking, parsing, prittifying, encoding, decoding and more:

I would say parsing it is the only way you can really entirely tell. Exception will be raised by python's json.loads() function (almost certainly) if not the correct format. However, the the purposes of your example you can probably just check the first couple of non-whitespace characters...

I'm not familiar with the JSON that facebook sends back, but most JSON strings from web apps will start with a open square [ or curly { bracket. No images formats I know of start with those characters.

Conversely if you know what image formats might show up, you can check the start of the string for their signatures to identify images, and assume you have JSON if it's not an image.

Another simple hack to identify a graphic, rather than a text string, in the case you're looking for a graphic, is just to test for non-ASCII characters in the first couple of dozen characters of the string (assuming the JSON is ASCII).