0

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.

2
  • Try using json.loads() instead of json.load() - parses a string instead of a file stream. Commented Apr 12, 2014 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. Commented Apr 12, 2014 at 12:20

3 Answers 3

1

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

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

3 Comments

@Backo Now read your code carefully. What are you indexing? What should you be indexing instead?
I don't understand your question. What do you mean exactly?
@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.
0

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

1 Comment

json.load does not expect a filename. It takes a file object.
0

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!

Comments

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.