-2

I have a json file with 15000+ lines that looks like this;

{
    "address": [
        "user address"
    ]
}{
    "purchase": [
        "details"
    ]
}{
    "info1": [
        "some information",
        "other information",
        "more information"
    ],
    "info2": [
        "something else"
    ]
}

I want to do something like:

f = open('my_file.json',)
data = json.load(f)
print(data[2])

In which index[2] would print everything between the last set of curly brackets so:

{
    "info1": [
        "some information",
        "other information",
        "more information"
    ],
    "info2": [
        "something else"
    ]
}

However I get error: raise JSONDecodeError("Extra data", s, end)

It works if I make it a string, something like: my_str = '[{"address": "user address"}, {"info1": "some information", "more information"}]' print(my_str[2])

Is it possible to read json file as above and print [2]?

Based on below comment, if I do something like this:

with open('my_file.json', 'r') as f:
my_list = list(f)
values_list = list(my_list)
a_value = values_list
print(a_value[10])

It prints "some information".

brick
  • 3
  • 2
  • The json file you entered in your question is not valid. If it does indeed look like the one above, you will have to fix it. – Hashim Abu Sharkh May 09 '22 at 02:16
  • Thanks Hashim, it was wrongly formatted when I pasted it here. Corrected it now. – brick May 09 '22 at 02:24
  • Wait, the edit changed the meaning of the question. Before the edit, you seemed to have one complete JSON on each line, which means you had a JSONL file, not a JSON file. If you in fact have multiple pretty-formatted JSON items in one file, that is neither JSON or JSONL, and is significantly harder to read — whoever created it has not thought it through. – Amadan May 09 '22 at 02:26
  • with the json file as is, if I do: with open('my_file.json', 'r') as f: my_list = list(f) v_list = list(my_list) a_value = values_list print(a_value[10]) it prints "some information" – brick May 09 '22 at 02:39
  • Could you please explain how, and what exactly I'm misrepresenting? How is the data incorrect? – brick May 09 '22 at 02:43
  • Never mind, I misread — you did not use `json.load` or `json.loads` in that code snippet. – Amadan May 09 '22 at 05:50

1 Answers1

0

json.load and json.loads need to have a correct JSON, from start to finish. Your file is not a correct JSON file; it seems it is a number of JSON documents concatenated with each other. To read it, you will have to be a bit more low-level:

with open('my_file.json', 'rt') as f:
    json_doc = f.read().strip()

decoder = json.JSONDecoder()
data = []
while json_doc:
    value, cont = decoder.raw_decode(json_doc)
    data.append(value)
    json_doc = json_doc[cont:]

print(data[2])
# => {'info1': ['some information', 'other information', 'more information'], 'info2': ['something else']}
Amadan
  • 191,408
  • 23
  • 240
  • 301