So I have to import a json file in an identical format shown below:
{
"name": "bob"
}
{
"name": "sarah"
}
This is the function I am trying to use to open it:
def read_json_file(file):
with open(file, "r") as r:
response = json.load(r)
return response
I am getting this error when trying to load it:
json.decoder.JSONDecodeError: Extra data: line 4 column 1 (char 22)
There's no way for me to fix the json data as the file is quite large. I need a way to work around it to parse through each dictionary.
I have already tried the method when this question was asked:
Python json.loads shows ValueError: Extra data
I tried changing my function to match the top answer:
response = json.dumps(r)
That however brought upon this error:
TypeError: Object of type TextIOWrapper is not JSON serializable
Any help would be appreciated on this.
In order solving that kind of "multiple"/"invalid" JSON, you can read the entire file, add these brackets []
to encapsulate the string and then load it as string with json.loads()
.
,
the intersection }{
, so it will be ...},{...
.[]
.json.loads()
to parse the JSON string.Full code:
def read_json_file(file):
with open(file, "r") as r:
response = r.read()
response = response.replace('\n', '')
response = response.replace('}{', '},{')
response = "[" + response + "]"
return json.loads(response)