I wrote a function to test 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 found that it allows the use of a numeric string. For instance,
is_json("123")
is_json("-123")
is_json("0123")
is_json(" 123")
In my opinion, a number should not be part of Json data. And I also confirmed this with another Json formatter tool . If true, why json.loadsallow row strings?
source
share