This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: json module loads function
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ammar2, jaihong
Priority: normal Keywords:

Created on 2018-09-18 13:18 by jaihong, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
json_test.py jaihong, 2018-09-18 13:18 the error test file
Messages (2)
msg325639 - (view) Author: zack (jaihong) Date: 2018-09-18 13:18
the json.loads function think a string variable is a json type, but the variable is "123"
msg325641 - (view) Author: Ammar Askar (ammar2) * (Python committer) Date: 2018-09-18 13:26
This is intentional, the parsed in JSON doesn't necessarily have to have an object at the root. This is also what allows you to do:

>>> json.loads("[]")
[]

This behavior is also consistent with browsers, try this in your browser's dev console:

> JSON.parse("123")
123
> JSON.parse('"123"')
"123"
> JSON.parse('[]')
[]

More technically, look at the JSON standard here: https://www.json.org/

As you can see, the root value is an "element", which is of type "value" surrounded by some whitespace. Values can be any of these:

value
    object
    array
    string
    number
    "true"
    "false"
    "null"
History
Date User Action Args
2022-04-11 14:59:06adminsetgithub: 78902
2018-09-18 13:26:54ammar2setstatus: open -> closed

nosy: + ammar2
messages: + msg325641

resolution: not a bug
stage: resolved
2018-09-18 13:18:10jaihongcreate