1

I have a .txt file with 70+k json object obtained by pulling data from twitter and dumping into file using:

with open("followers.txt", 'a') as f:
     for follower in limit_handled(tweepy.Cursor(api.followers, screen_name=account_name).pages()):
         for user_obj in follower:
             json.dump(user_obj._json, f)  
             f.write("\n")  

When I try to read this in python using the code below:

import json
with open('followers.txt') as json_data:
     follower_data = json.load(json_data)

I get error:

ValueError: Extra data: line 2 column 1 - line 2801 column 1 (char 1489 - 8679498)

It worked when I read a test file with one json object copied from the original file using the same code above. Once I add a second json object to this file then using the same code above gives the error:

ValueError: Extra data: line 2 column 1 - line 2 column 2376 (char 1489 - 3864)

How do I read the file with more than one json object?

1
  • You're not writing it correctly, so you cannot read it correctly. Commented Jul 19, 2017 at 6:17

2 Answers 2

2

The issue comes when you write your JSON. You must write a single JSON object, so you may also load a single JSON object. Currently, you are writing multiple separate objects, causing the error.

Modify your write code a bit:

json_data = []
with open("followers.txt", 'a') as f:
     for follower in limit_handled(tweepy.Cursor(api.followers, screen_name=account_name).pages()):
         for user_obj in follower:
             json_data.append(user_obj._json)             

     # outside the loops
     json.dump(json_data, f)  

Now, when reading, your existing code should work. You'll get a list of dictionaries.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for pointing this out @cᴏʟᴅsᴘᴇᴇᴅ. I have updated my write code.
@T-Jay if you used my answer then you should accept mine man ;p Just kidding.
1

Of course it is best to treat the problem from the root: write a single json and read it, as suggested by COLDSPEED.
However, if you already wrote multiple json objects into a single file, you can try the following code to use the already created file:

import json
follower_data = []  # a list of all objects
with open('followers.txt') as json_data:
  for line in json_data:
    follower_data.append( json.loads(line) ) 

Assuming you did not indent your json objects when you wrote them to 'flowers.txt', then each line in the file is a json object that can be parsed independantly.

1 Comment

thanks, this works perfectly to load data already written in multiple json objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.