Extracting JSON element


Extracting JSON element



I have a JSON response from the website shown below. I want to print the 'value' and 'datetime' keys of data. I am not able to access these two elements in JSON response.


'value'


'datetime'


data


data= {"parameter_name":"Inst",
"parameter_code":"WL1","data":[
{"value":3.1289999485,"datetime":"2018-07-01T00:00:00+00:00"},
{"datetime":"2018-07-01T00:30:00+00:00","value":3.1859998703},
{"value":3.33099985123,"datetime":"2018-07-01T00:45:00+00:00"},
{"datetime":"2018-07-01T01:15:00+00:00","value":3.22300004959},
{"datetime":"2018-07-01T01:45:00+00:00","value":3.32299995422}]}



my code till now


for element in len(data['data']):
date = element['datetime']
value = element['value']

print value, date



I am getting error


for element in len(data['data']):
TypeError: string indices must be integers, not str




3 Answers
3



What you've shown as your JSON data is likely not the actual value of data. If you attempt to access the data like a Python dict, it raises TypeError: string indices must be integers, not str. Your JSON data probably looks like this (notice the quotes):


data


data


dict


TypeError: string indices must be integers, not str


data


# This is JSON, essentialy a string in the format of a Python dict.
data = """{
"parameter_name": "Inst",
"parameter_code": "WL1",
"data":[
{
"value":3.1289999485,
"datetime":"2018-07-01T00:00:00+00:00"
},
{
"datetime":"2018-07-01T00:30:00+00:00",
"value":3.1859998703
},
{
"value":3.33099985123,
"datetime":"2018-07-01T00:45:00+00:00"
},
{
"datetime":"2018-07-01T01:15:00+00:00",
"value":3.22300004959
},
{
"datetime":"2018-07-01T01:45:00+00:00",
"value":3.32299995422
}
]
}"""



Convert it into a Python dict by using the Python Standard Library json package:


dict


json


import json

# This converts the JSON string into a Python dict
data = json.loads(data)



You can access the data and it's 'data' key, then iterate over it (like you were doing):


data


for element in data['data']:
print(element['value'], element['datetime'])





Thanks for the help.. got it. I also saw stackoverflow.com/questions/37986406/…
– gi.rajan
Jul 1 at 3:01





@gi.rajan Yep, same idea as that question/solution. Glad I could help :-)
– Cole
Jul 1 at 3:03



You can try like this:


for element in data['data']:
date = element['datetime']
value = element['value']

print(date)
print(value)



Output:


3.1289999485
2018-07-01T00:00:00+00:00
3.1859998703
2018-07-01T00:30:00+00:00
3.33099985123
2018-07-01T00:45:00+00:00
3.22300004959
2018-07-01T01:15:00+00:00
3.32299995422
2018-07-01T01:45:00+00:00



Explanation:



If you want to iterate over the elements in the list,:


list


for element in data['data']



If you want to iterate over the list using by their index:


for index in range(len(data['data'])):



If you have a web responce in text format you would also have to decode it first. Check
https://docs.python.org/2/library/json.html (for python 2) or https://docs.python.org/3.7/library/json.html (for python 3) to see the documentation about the json library.
You have to:


json


import json
decodedData = json.loads(data)



and then loop over decodedData as you've done.





Thanks for pointing me in the right direction...
– gi.rajan
Jul 1 at 3:02






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

List of Kim Possible characters

Python Tkinter Error, “Too Early to Create Image”