0

I have JSON files, which contains two dictionaries, which are not separated by any delimiter. I need to get each of those dictionaries as individual JSON objects. How can I go about doing this? I am working in Python.

The files looks like

{"data":[[], [], ...., []]}{"data":[[], [], ..., []]}

The file could contains like 10K inner arrays.

I tried reading the file in line format, as I saw in a few solutions here and there but none of them are working.

data = json.load(f)

gives me the error

json.decoder.JSONDecodeError: Extra data: line 1 column 115451 (char 115450)

I also tried

data = []
with open('data.json', 'r') as f:
    for line in f:
        data.append(json.loads(line))

but it gives me the same error,

json.decoder.JSONDecodeError: Extra data: line 1 column 115451 (char 115450)

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • please post the json – Xyndra Mar 19 '23 at 13:04
  • Can't share the exact file, bounded by some conditions, have added the outline of the json – not_coding Mar 19 '23 at 13:06
  • [this might be a different possible solution: https://stackoverflow.com/questions/47060035/python-parse-json-array](https://stackoverflow.com/questions/47060035/python-parse-json-array) – Xyndra Mar 19 '23 at 13:07
  • That won't work because the dictionaries in that json are inside a list, so can be read as list elements, which is not in my case – not_coding Mar 19 '23 at 13:09
  • does the data contain `{` or `}`? – Xyndra Mar 19 '23 at 13:12
  • 1
    Does this answer your question? [How to retrieve JSON objects from a text file using Python where the objects are \*not\* separated by a delimiter?](https://stackoverflow.com/questions/8730119/how-to-retrieve-json-objects-from-a-text-file-using-python-where-the-objects-are) – Gino Mempin Mar 21 '23 at 03:19

1 Answers1

0

you could try reading the file as text first and then parse it, but the data may not contain { or }:

import re

text = "{abc}{cd:'ef'}" # change this(might have to remove newlines)

print(re.findall('{.*}', text))

|
|

[{abc},{cd:"ef"}]
Xyndra
  • 110
  • 1
  • 11