Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I am getting a bit of headache just because a simple looking, easy statement is throwing some errors in my face.

I have a json file called strings.json like this:

"strings": [ {"-name": "city", "#text": "City"}, {"-name": "phone", "#text": "Phone"}, ..., {"-name": "address", "#text": "Address"} ]

then I want to read it, just that by now. I have this statements which I found out, but it's not working:

import json
from pprint import pprint

with open('strings.json') as json_data:
    d = json.loads(json_data)
    json_data.close()
    pprint(d)

The error spitted in my face was this:

Traceback (most recent call last):
File "/home/tegris/Downloads/i18n_fieldlink/i18n_fieldlink/android/values/manipulate_json.py", line 5, in <module>
d = json.loads(json_data)
File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer
[Finished in 0.1s with exit code 1]

EDITED

Changed from json.loads to json.load

and got this:

Traceback (most recent call last):
File "/home/tegris/Downloads/i18n_fieldlink/i18n_fieldlink/android/values/manipulate_json.py", line 5, in <module>
d = json.load(json_data)
File "/usr/lib/python2.7/json/__init__.py", line 278, in load
**kw)
File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 369, in decode
raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 829 column 1 - line 829 column 2 (char 18476 - 18477)
[Finished in 0.1s with exit code 1]
share|improve this question
1  
Are you sure that the file contains valid JSON? – Explosion Pills Nov 25 '13 at 17:16
    
possible duplicate of Parsing values from a JSON file in Python – Pureferret May 19 at 9:39

3 Answers 3

up vote 37 down vote accepted

You are using the json.loads method. More documentation here. This method is used for string arguments only. Luckily, there is a similarly named json.load method documented here. This one can be used directly on a file object.

Edit: The new message is a totally different problem. In that case, there is some invalid json in that file. For that, I would recommend running the file through a json validator.

share|improve this answer
    
hm...I changed from json.loads to json.load but I get that nice msg. – Doug Nov 25 '13 at 17:23
2  
Ah, well the new message is a totally different problem. In that case, there is some invalid json in that file. For that, I would recommend running the file through a json validator. – ubomb Nov 25 '13 at 17:26
    
got it! The file was missing EOF. The file was not correctly ended. I wouldn't notice that if it wasn't your good recommendation! Thanks! – Doug Nov 25 '13 at 17:32
    
ubomb, if you can change you answer to me to mark it as accepted. Be free! I'll mark it. – Doug Nov 25 '13 at 17:33
    
@Doug Done. Thanks! – ubomb Nov 25 '13 at 17:35

Here is a copy of code which works fine for me

import json

with open("test.json") as json_file:
    json_data = json.load(json_file)
    print(json_data)

with the data

{
    "a": [1,3,"asdf",true],
    "b": {
        "Hello": "world"
    }
}

you may want to wrap your json.load line with a try catch because invalid JSON will cause a stacktrace error message.

share|improve this answer

The problem is using with statement:

with open('strings.json') as json_data:
    d = json.load(json_data)
    pprint(d)

The file is going to be implicitly closed already. There is no need to call json_data.close() again.

share|improve this answer
    
Please remove the json_data.close(). As mentioned, it will be called implicitly. – Bonnie Varghese Nov 22 '14 at 7:31
    
Thanks @BonnieVarghese for pointing out. I corrected above – Zongjun Dec 2 '14 at 20:28
    
@Zongjun : Please correct loads to json.load(json_data). – Knight71 May 18 at 15:28

protected by Tats_innit Nov 11 at 1:26

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.