1

I am getting data from an API call using the following Python code.

import urllib.request
import json

response = urllib.request.urlopen(req)
string = response.read().decode('utf-8')
json_obj = json.loads(string)
print(json_obj) 

The result set is as below:

{"forecast": [645.946539419183], "index": [{"Date": 1629072000000, "ProductType": "Type1"}]}

How can I just return the forecast value?

645.946539419183

I have tried many things to get the value but nothing seems to be working

print(json_obj['forecast'][0]) 

Throws the following error:

TypeError                                 Traceback (most recent call last)
<ipython-input-44-af91dd9fcc96> in <module>
     37     json_obj = json.loads(string)
     38 
---> 39     print(json_obj['forecast'][0])
     40 
     41 except urllib.error.HTTPError as error:

TypeError: string indices must be integers
15
  • How can json_obj be a string when it is the return from json.loads()? Commented Aug 21, 2021 at 17:10
  • 1
    Looks like the error you shared does not match the code you have posted. Commented Aug 21, 2021 at 17:10
  • @balderman I have double checked and the code and error message are an exact match Commented Aug 21, 2021 at 17:13
  • 1
    Could you please show the output of type(json_obj) please? Your information seems incoherent with the documentation... Commented Aug 21, 2021 at 17:14
  • @BlackBeans good call, it returns this print(type(json_obj)) return <class 'str'> Commented Aug 21, 2021 at 17:18

3 Answers 3

2
json_obj = json.loads(json.loads(string))
json_obj["forecast"][0]

The second indexing is required because the value you seek is in a list.

This works, but I want to stress out that there is probably a bug in the service that provides the string, since it encoded twice the data...

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

5 Comments

This is also throwing an error " except urllib.error.HTTPError as error: ypeError: string indices must be integers"
throwing a different error this time ` JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1) `
@M.Ali Ok, last attempt, try this one.
Thank you very much for your help mate, really appreciate you helping me with this :D the last one has worked :D
@M.Ali I don't know if you are the one coding the API from which you are retrieving the information, but it is very likely a bug that the information was encoded twice. Try patching the bug, then the previous solution should work.
0
json_obj = {"forecast": [645.946539419183], "index": [{"Date": 1629072000000, "ProductType": "Type1"}]}
print(json_obj['forecast'][0])

The code above works.

2 Comments

This is also throwing an error " except urllib.error.HTTPError as error: ypeError: string indices must be integers"
see my comment above - the error and the code does not match! Can you share the url so we can test it?
0

You can do it like this.

forecast = json_obj['forecast']

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.