简体   繁体   中英

How do I load a list of json objects from a .txt file?

I am trying to load a list of json objects from a text file that I have been given. When I try to do this I get the error "json.decoder.JSONDecodeError: Extra data: line 1 column 14826 (char 14825)". I believe that this is because each json object is wrapped in quotes and separated by a comma.

for example the file I am reading looks like this

"{'name': 'john', age: '17', sex: 'M', 'nums': [2, 3, 4]}","{'name': 'sam', age: '23', sex: 'F', 'nums': [2, 3, 4]}","{'name': 'max', age: '12', sex: 'M', 'nums': [2, 3, 4]}"

I have tried using json.loads() and ast.literal_eval(), but both give me the same problem. I have also tried replacing the "," with a , and nothing to see if that would make it the correct format but the same errors came up.

data = []
with open("raw.txt", encoding = 'utf8') as f:
            data = f.read()

data = json.loads(data)

I am expecting to get a list of all of the json objects in the text file

You were right regarding the potential of the ast module for this task. Using your example (in which I corrected the sex and age keys quoting), you don't seems to have valid JSON values but it seems you have valid representations of python dict s.

data = '''"{'name': 'john', 'age': '17', 'sex': 'M', 'nums': [2, 3, 4]}","{'name': 'sam', 'age': '23', 'sex': 'F', 'nums': [2, 3, 4]}","{'name': 'max', 'age': '12', 'sex': 'M', 'nums': [2, 3, 4]}"'''

By adding parenthesis on the start/end of this string, it can be parsed as a tuple of string. Then each of these strings will be parsed as a python dict :

import ast

b = ast.literal_eval('(' + data + ')')
b = [ast.literal_eval(elem) for elem in b]

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