Theresa Theresa - 5 days ago 6
JSON Question

Does digits string belong to Json?

I wrote a function to validate Json objects in Python. Here is the code snippet:

def is_json(myjson):
"""
Check whether a string is a json object.
:param myjson: the string to check
:return: True/False
"""
try:
json_object = json.loads(myjson)
except ValueError, e:
return False
return True


However, I happened to find that it allows digit string. For instance,

is_json("123") # return True
is_json("-123") # return True
is_json("0123") # return False
is_json(" 123") # return False


From my understanding, number should not be part of Json data. And I also confirmed it with some other Json formatter tool. If it is true, why would
json.loads
permit digits strings?

Answer

It looks like Python's json module supports JSON specified by RFC 7159:

JSON (JavaScript Object Notation), specified by RFC 7159 (which obsoletes RFC 4627)

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".

The 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.