4

This is the first time I try to use a JSON file in Python and although I have read a lot of stuff regarding this matter, I am still very confused. I want to read a file named jason.json line by line, store it to a list named data and then print it. However, i always get the following error :

Traceback (most recent call last):
  File "try.py", line 6, in <module>
    data.append(json.loads(line))
  File "C:\Users\...\Python35\lib\json\__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "C:\Users\...\Python35\lib\json\__init__.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\...\Python35\lib\json\__init__.py", line 355, in raw_decode
    obj, end = self.scan_once(s, idx)
  json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 1 (char 2)

This is my code:

import json
data = []
with open('jason.json') as f:
    for line in f:
        data.append(json.loads(line))

print(data)

And this is jason.json:

{
  "E(2,3)" : "A common Afro-Cuban drum pattern",
  "E(2,5)" : "A rhythm found in Greece",
  "E(3,4)" : "It is the archetypal pattern of the Cumbia from Colombia",
  "E(3,5)" : "Persian rhythm"
}

Thank you in advance!

4
  • You need to load the whole file in one go; each individual line is not valid JSON. Commented May 14, 2016 at 14:55
  • @Martijn Pieters solution is the way to go. However, the reason you have this problem is that you probably created the file manually or copied that from else where such that it has line breaks. Normally .json files do not have line breaks, and therefore your only line would be valid json. You can save a data structure to a file with json.dump(my_data, my_file) it will no have line breaks, and you could (though not advised) load it using your approach. Commented May 14, 2016 at 15:26
  • @Akavall: no, JSON easily can have line breaks. Use json.dump(my_data, my_file, indent=4) for example. JSON was never designed to be treated as a single-line format, really. Commented May 14, 2016 at 17:41
  • @MartijnPieters, I see. Thank you for the information. Commented May 14, 2016 at 19:00

2 Answers 2

7

Note that the data stored in jason.json is a dict and not a list. So, if you want a list of tuples, you can do something like

with open('jason.json') as f:
    data = list(json.load(f).items())
print(data)

which yields

[('E(3,5)', 'Persian rhythm'), ('E(3,4)', 'It is the archetypal pattern of the Cumbia from Colombia'), ('E(2,3)', 'A common Afro-Cuban drum pattern'), ('E(2,5)', 'A rhythm found in Greece')]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this really helped!
4

You are reading your file line by line, but a single line from your file is not a valid JSON document:

>>> import json
>>> json.loads('{\n')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python3.5/json/__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python3.5/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python3.5/json/decoder.py", line 355, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 1 (char 2)

Decode the whole file in one go; this is easiest done with the json.load() function:

with open('jason.json') as f:
    data.append(json.load(f))

5 Comments

I think it is data = json.load(f) data does not yet exist
@PyNEwbie: In the question, data = [] is defined before the with statement. I've merely focused on the with block.
I also think he does not know what a json object is he seems to think adding the key,value pairs is what data should be
@PyNEwbie: that's just speculation; we have no evidence either way.
@MartijnPieters Oh I didn't know that.Thank you very much!

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.