0

I am trying to read the JSON file created by Tweet.py. However, whatever I tried I am receiving an ValueError consistently.

ValueError: Expecting property name: line 1 column 3 (char 2)

JSON results are in the format of:

{ 'Twitter Data' : [ {
"contributors": null, 
"coordinates": null, 
"created_at": "Tue Oct 24 15:55:21 +0000 2017", 
"entities": {
    "hashtags": ["#football"]
   }            
} , {
"contributors": johnny, 
"coordinates": null, 
"created_at": "Tue Oct 24 15:55:21 +0000 2017", 
"entities": {
    "hashtags": ["#football" , "#FCB"]
   }             
} , ... ] }

There are at least 50 of these JSON objects in the file, which are separated by commas.

My Python script to read this json file is:

twitter_data=[]
with open('@account.json' , 'r') as json_data:
    for line in json_data:
        twitter_data.append(json.loads(line))

print twitter_data

Tweet.py writes these Json objects by using:

json.dump(status._json,file,sort_keys = True,indent = 4) 

I would appreciate any help and guidance on how to read this file!

Thank you.

3
  • 1
    Would you please provide a complete, but much smaller sample JSON file? Commented Oct 24, 2017 at 20:53
  • Yes, of course @ChristopherBottoms Commented Oct 24, 2017 at 20:56
  • 1
    1) ' is not a legal string delimter in JSON, and 2) your code expects a new JSON doc every line, but your sample is a single JSON doc that happens to take multiple lines. Commented Oct 24, 2017 at 21:18

2 Answers 2

1

The { 'Twitter Data' bit should be { "Twitter Data" as well as "Johnny"

That is to say keys and values (strings) must be enclosed in double quotes.

with open("@account.json","r") as json_data:
    data = json_data.readlines()
    twitter_data.append(json.loads(data))

Also, Haven't used this myself but this might be of help as well: https://jsonlint.com

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

1 Comment

I got the following error when I tried this: in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) TypeError: expected string or buffer
0

First off, as both @Rob and @silent have noted, 'Twitter Data' should be "Twitter Data". Json needs double quotes, not single quotes to delimit a string.

Secondly, when reading with json.load() it expects a file Object, so when calling json.load(), just pass in json_data and it will read the whole json file into memory:

with open('@account.json' , 'r') as json_data:
    contents = json.load(json_data)

EDIT:

for handling multiple objects at once:

def get_objs(f):
    content = f.read()

    # Get each object in the contents of the file object.
    # This is kinda clunky and inelegant, but it should work
    objs = ['{}{}'.format(i, '}') for i in content.split('},')]

    # Last json_obj probably got an unnecessary "}" at the end, so trim the
    # last character from it
    objs[-1] = objs[-1][0:-1]

    json_objs = [json.loads(i) for i in objs]
    return json_objs

and then just go:

with open('@account.json', 'r') as json_data:
    json_objs = get_objs(json_data)

Hopefully this will work for you. It did for me when I tested it on a simalarly formatted json file.

5 Comments

I made the edits and just tried. I got the following error: ValueError: No JSON object could be decoded
Ah, I notice now you said there would be multiple json objects in each. I have came up with a possible solution. Editing now.
@Pythoner1234: The multiple objects you refer to are the 'multiple' objects within the list key Twitter Data right? or are there multiple Twitter Data objects? can you list 2 or 3 objects so that we are clear on the structure of the JSON file.
I got the following error when I attempted this: anaconda/lib/python2.7/json/decoder.py", line 382, in raw_decode raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded
Can you provide a better example of your json file like @silent asked so we better know the structure of the file?

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.