JeBo
JeBo

Reputation: 197

Loading JSON and getting certain data (Python)

I've been collecting some tweets into a JSON file, with which I need to do some statistics with certain data in the JSON. After Googling several options of how to do this, none could give me the correct solution.

The JSON looks like this:

{"contributors": null, "truncated": false, "text": .... }

And applied this code to try and load it:

 import json
 f = open("user_timeline_Audi.jsonl",'r')
 data = f.read()
 print(data)
 bla = json.loads(data)

Basically the json.loads() gives me the next error:

json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 2698)

The end goals is that I need to get the followers_count and likes from several JSON files. Hope that someone can help!

EDIT:

Based on the answer from Alex Hall, my code now is:

import json

with open("user_timeline_BMW.jsonl",'r') as f:
    for line in f:
    obj = json.loads(line)
    bla = ["followers_count"]
    print(bla)

This just outputs a list, instead of the values behind it:

....
['followers_count']
['followers_count']
....

Hope someone has a suggestion for this step!

Upvotes: 1

Views: 67

Answers (2)

postmalloc
postmalloc

Reputation: 90

I think it is supposed to be bla = obj["followers_count"]

Upvotes: 1

Alex Hall
Alex Hall

Reputation: 36013

You are dealing with JSON lines, where each line contains one JSON object. You should do:

for line in f:
    obj = json.loads(line)

and then do what you want with each object.

Upvotes: 4

Related Questions