gis.rajan
gis.rajan

Reputation: 517

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.

  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

Upvotes: 1

Views: 293

Answers (3)

Cole
Cole

Reputation: 1745

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):

# 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:

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):

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

Upvotes: 2

user9886608
user9886608

Reputation:

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:

import json
decodedData = json.loads(data)

and then loop over decodedData as you've done.

Upvotes: 1

user5777975
user5777975

Reputation:

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,:

 for element in data['data']

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

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

Upvotes: 2

Related Questions