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 wrote my first Python script (note: I am new to Python) and I am trying to parse some JSON string in order to retrieve a value related to a particular JSON key but I am in trouble.

In my script.py file I have the following:

data = '{ "key1": "152", "key2": "da8fb07ace5512", "key3": "cfed379e13aebc" }'
data_decoded = json.load(data)
data["key1"]

When I run the above script with the command python script.py then I get:

Traceback (most recent call last):
  File "/script.py", line 2, in <module>
    data_decoded = json.load(data)
  File "/usr/lib/python2.7/json/__init__.py", line 274, in load
    return loads(fp.read(),
AttributeError: 'str' object has no attribute 'read'

How can I solve the problem? I expect that the returned value is 152.


UPDATE (after commenting)

If I use loads() then I get:

Traceback (most recent call last):
  File "/script.py", line 3, in <module>
    data["key1"]
TypeError: string indices must be integers, not str

I don't care if it is a string or a integer. I would like just to retrieve the value.

share|improve this question
    
Try using json.loads() instead of json.load() - parses a string instead of a file stream. – Dietrich Apr 12 '14 at 12:06
    
"I don't care if it is a string or a integer." - it's not a value that is a string or integer. You see the "indices" word in the error message? You are supposed to read and understand error messages, that's why they are here. They help you. – The Paramagnetic Croissant Apr 12 '14 at 12:20

3 Answers 3

up vote 0 down vote accepted

json.load expects a filename file-like object. You want to use json.loads which loads from a string.

See also: https://docs.python.org/2/library/json.html

Edit: Try data_decoded['152']. You're still trying to access data, the JSON string

share|improve this answer
    
json.load does not expect a filename. It takes a file object. – The Paramagnetic Croissant Apr 12 '14 at 12:07
    
I updated the question since I am still in trouble. – Backo Apr 12 '14 at 12:12

For strings, you have to use json.loads(), not json.load(), because load is for files and file-like objects.

share|improve this answer
    
I updated the question since I am still in trouble. – Backo Apr 12 '14 at 12:13
    
@Backo Now read your code carefully. What are you indexing? What should you be indexing instead? – The Paramagnetic Croissant Apr 12 '14 at 12:16
    
I don't understand your question. What do you mean exactly? – Backo Apr 12 '14 at 12:17
    
@Backo I mean you should read your code and think about what it is doing. data["key1"] doesn't make sense. data_decoded["key1"] does. Please show a bit of effort debugging your own code, especially since this is a trivial error. – The Paramagnetic Croissant Apr 12 '14 at 12:19
    
Ops!!! I was in fault! – Backo Apr 12 '14 at 12:22

You are already declaring your dictionary object, just take it out of quotation marks:

data = { "key1": "152", "key2": "da8fb07ace5512", "key3": "cfed379e13aebc" }

Because you already have a dictionary there is no need to do a json function, just simply reference the data:

data['key1']

However, if I am misunderstanding your question and you are passing in the data as a json, python will recognize it as a string and you need to use a json.loads function on the data:

Note that after you issue the data_decoded = json.loads(data), your dictionary object is no longer data, it is actually now data_decoded. Did you try:

data_decoded['key1'] 

???

Good luck!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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