I wrote a function to validate Json objects in Python. Here is the code snippet:
Check whether a string is a json object.
:param myjson: the string to check
json_object = json.loads(myjson)
except ValueError, e:
is_json("123") # return True
is_json("-123") # return True
is_json("0123") # return False
is_json(" 123") # return False
It looks like Python's
json module supports JSON specified by RFC 7159:
Playing around with the validator you linked, "123" is invalid in 4627, but valid in 7159.
Comparing the two RFCs, it looks like the definition of what comprises valid JSON text became more broad. In 4627:
JSON-text = object / array
And in 7159:
JSON-text = ws value ws
(ws meaning "optional whitespace")
So originally you could only have an object or an array, but now you can have a value, which is "an object, array, number, or string, or one of the [...] names: false null true".
json module documentation discusses the difference further:
The old version of JSON specified by the obsolete RFC 4627 required that the top-level value of a JSON text must be either a JSON object or array (Python dict or list), and could not be a JSON null, boolean, number, or string value. RFC 7159 removed that restriction, and this module does not and has never implemented that restriction in either its serializer or its deserializer.