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".
CodePudding user response:
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']}