I have a .json file in Python that looks like this:
{
"abc_id": "ABC-2019-1945",
"def_id": "20"
},
{
"abc_id": "ABC-2019-1944",
"def_id": "200"
}
I'm trying to read it using json.load(open(json_path,"r"))
but it gives me an error since it has multiple .json objects.
It looks like simply adding [
at the beginning and ]
at the end of the file would solve the problem, but I don't know how to add anything to the file that I can't read (doing it by hands for every file is a bad solution for me). I also tried to read the file as a text file, but I didn't figure out how to read .json as a text.
Could anyone suggest me a good solution for my problem please?
The error is because the JSON is invalid though not just because of the missing [] but because the comma at the end of the def_id key within each entry is wrong. The last element within each object must not end with a comma.
You can run it through https://jsonlint.com/ to check.
valid
json.loads('[{"abc_id": "ABC-2019-1945", "def_id": "20"}, {"abc_id": "ABC-2019-1944", "def_id": "200"}]')
invalid due to extra comma
json.loads('[{"abc_id": "ABC-2019-1945", "def_id": "20",}, {"abc_id": "ABC-2019-1944", "def_id": "200",}]')
So the original JSON need to be this
[
{
"abc_id": "ABC-2019-1945",
"def_id": "20"
},
{
"abc_id": "ABC-2019-1944",
"def_id": "200"
}
]
** update ** now that the question has been amended to include valid JSON you can use
with open(json_path,"r") as f:
d = f.read()
json.loads(f"[{data}]")
# or
json.loads("[" + data + "]")
# or
json.loads("[%s]" % data)
# or
json.loads("[{}]" % data)
json module provides two methods to parse JSON objects (and same applies to serialization as well):
Here is the final code:
with open(json_path,"r") as f:
data = f.read()
json.loads("["+data+"]")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.