Reading a JSON file with multiple objects in Python

I am a bit of an idiot in programming and Python. I know that there are many explanations in previous questions about this, but I carefully read them all and I could not find a solution.
I am trying to read a JSON file containing about 1 billion data, such as:

334465|{"color":"33ef","age":"55","gender":"m"}
334477|{"color":"3444","age":"56","gender":"f"}
334477|{"color":"3999","age":"70","gender":"m"}

I tried to overcome these 6-digit numbers at the beginning of each line, but I don’t know how I can read multiple JSON objects? Here is my code, but I can not find why it is not working?

import json

T =[]
s = open('simple.json', 'r')
ss = s.read()
for line in ss:
    line = ss[7:]
    T.append(json.loads(line))
s.close()

And here is the error I received:

ValueError: Extra Data: line 3 column 1 - line 5 column 48 (char 42 - 138)

Any suggestion would be very helpful for me!

+4
source share
3 answers

There are several problems with the logic of your code.

ss = s.read()

s .

for line in ss:

. , line .

    line = ss[7:]

7 ( 0 6 ) line .

T.append(json.loads(line))

JSON T.


, , . .read .readlines, for .

with, , with -.

import json

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

for row in table:
    print(row)

{'color': '33ef', 'age': '55', 'gender': 'm'}
{'color': '3444', 'age': '56', 'gender': 'f'}
{'color': '3999', 'age': '70', 'gender': 'm'}

, table :

import json

with open('simple.json', 'r') as f:
    table = [json.loads(line[7:]) for line in f]

for row in table:
    print(row)
+2

readlines() read() JSON try/except. , , , .

s = open('simple.json', 'r')
for line in s.readlines():
    try:
        j = line.split('|')[-1]
        json.loads(j)
    except ValueError:
        # You probably have bad JSON
        continue
0

Thank you very much! You guys are life savers! This is the code that I eventually came up with. I think this is a combination of all the answers!

import json

table = []
with open('simple.json', 'r') as f:
    for line in f:
        try:
            j = line.split('|')[-1]
            table.append(json.loads(j))
        except ValueError:
            # You probably have bad JSON
            continue

for row in table:
    print(row)
0
source

Source: https://habr.com/ru/post/1661396/


All Articles