Does the string of numbers belong to Json?

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") # return True
is_json("-123") # return True
is_json("0123") # return False
is_json(" 123") # return True

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?

+4
source share
1 answer

It looks like the Python jsonmodule supports the JSON specified by RFC 7159:

JSON (JavaScript Object Notation) specified by RFC 7159 (which is deprecated by RFC 4627)

When playing with a validator connected, "123" is not valid in 4627, but is valid in 7159.

RFC, , , JSON, . 4627 :

JSON-text = object / array

7159:

JSON-text = ws value ws

(ws " " )

, , , ", , " [...]: false null true ".

json :

JSON, RFC 4627, , JSON JSON (Python dict list), JSON null, boolean, . RFC 7159 , , .

+4

Source: https://habr.com/ru/post/1662593/


All Articles