V
V
Vlad Zaitsev2021-11-19 01:52:58
Python
Vlad Zaitsev, 2021-11-19 01:52:58

Why does json.load consider the textual representation of digits to be valid json?

>>> json_object = json.loads("60")
>>> json_object
60

json.loads(60) is an error, json.loads("aa") is an error, but json.loads("60") is no error does not give out, and I cannot distinguish json from a string with numbers. Two questions:
1) Why is that?
2) How to distinguish json from a number if I received a text string?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Aetae, 2021-11-19
@Aetae

Obviously a literal "60"is a string 60. String 60is json from number 60.
Json for a string ааis a string "аа", and a literal for such a string would be "\"аа\"".
As data in memory, by itself, pure strings are stored, so you will not have problems with data coming from outside.

R
rPman, 2021-11-19
@rPman

according to the standard , json is a constant in javascript format, which just fall under numbers

value = false / null / true / object / array / number / string

in practice, passing only a number is almost meaningless (although you can find a problem for strings), so everything that does not have an object or array format can not be considered json (I do not advise you to tie the format in your program through the type of data being transferred, better make it stricter )
so that this to determine whether it is necessary to parse json (although it is simple and modern parsers are terribly fast), it is enough to check the first non-empty character of the string for '[' or '{'

A
Alexander, 2021-11-19
@Survtur

Regarding the first question from the wiki :
The following can be used as values ​​in JSON:

  1. an entry is an unordered set of key:value pairs enclosed in curly braces "{ }". The key is described by a string, between it and the value there is a ":" symbol. Key-value pairs are separated from each other by commas. Example:{"hasBrain": false", "name": "Zombie"}
  2. an array (one-dimensional) is an ordered set of values. The array is enclosed in square brackets "[ ]". Values ​​are separated by commas. The array can be empty, that is, it can contain no value. Values ​​within the same array can have different types. Example:[1, 2, "a", 7, true, {"a": "b"}]
  3. number (integer or real). Example:-11.2
  4. literals true(boolean true), false(boolean false), and null.
  5. string is an ordered set of zero or more unicode characters enclosed in double quotes. Characters can be specified using escape sequences beginning with a backslash "\" (supported variants are \", \\, \/, \t, \n, \r, \f, and \b), or written in hexadecimal code encoded in Unicode as \uFFFF Example:"asdasd"

"60"- this is point 5
aa- this is none of the points.
On the second question:
1. If json.load caused an error, then it's definitely not json.
2. But any json is a string by definition, because it is a text data format.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question