简体   繁体   中英

How do I read a .json file that doesn't have [ ]?

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):

  • json.load - reads the JSON string from the file object <- this is the one you were using by default. You can't do much here, unless you open file first, read data, add brackets and put it back into the file. Then you can read it again and it will be parsed properly. Definitely not the best way to handle this issue, but as someone else commented - it might be better to fix it where it gets written into the file. json.dump is used to write serialized object into a file.
  • json.loads - 's' stands for string, meaning that this method takes a string as an input. json.dumps will return a serialized JSON object as a string.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM