Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing JSON elements

Tags:

python

json

I am getting the weather information from a URL.

weather = urllib2.urlopen('url')
wjson = weather.read()

and what I am getting is:

{
  "data": {
     "current_condition": [{
        "cloudcover": "0",
        "humidity": "54",
        "observation_time": "08:49 AM",
        "precipMM": "0.0",
        "pressure": "1025",
        "temp_C": "10",
        "temp_F": "50",
        "visibility": "10",
        "weatherCode": "113",
        "weatherDesc": [{
            "value": "Sunny"
        }],
        "weatherIconUrl": [{
            "value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png"
        }],
        "winddir16Point": "E",
        "winddirDegree": "100",
        "windspeedKmph": "22",
        "windspeedMiles": "14"
    }]        
 }
}

How can I access any element I want?

if I do: print wjson['data']['current_condition']['temp_C'] I am getting error saying:

string indices must be integers, not str.

like image 323
doniyor Avatar asked Apr 21 '13 09:04

doniyor


People also ask

How do I access JSON elements?

Below is a JSON string. To access the JSON object in JavaScript, parse it with JSON. parse() , and access it via “.” or “[]”.

How can I get specific data from JSON?

Getting a specific property from a JSON response object Instead, you select the exact property you want and pull that out through dot notation. The dot ( . ) after response (the name of the JSON payload, as defined arbitrarily in the jQuery AJAX function) is how you access the values you want from the JSON object.

How do you access JSON elements in python?

It's pretty easy to load a JSON object in Python. Python has a built-in package called json, which can be used to work with JSON data. It's done by using the JSON module, which provides us with a lot of methods which among loads() and load() methods are gonna help us to read the JSON file.

What are the JSON elements?

JSON defines seven value types: string, number, object, array, true, false, and null.


3 Answers

import json
weather = urllib2.urlopen('url')
wjson = weather.read()
wjdata = json.loads(wjson)
print wjdata['data']['current_condition'][0]['temp_C']

What you get from the url is a json string. And your can't parse it with index directly. You should convert it to a dict by json.loads and then you can parse it with index.

Instead of using .read() to intermediately save it to memory and then read it to json, allow json to load it directly from the file:

wjdata = json.load(urllib2.urlopen('url'))
like image 61
Yarkee Avatar answered Oct 10 '22 15:10

Yarkee


Here's an alternative solution using requests:

import requests
wjdata = requests.get('url').json()
print wjdata['data']['current_condition'][0]['temp_C']
like image 34
alecxe Avatar answered Oct 10 '22 15:10

alecxe


'temp_C' is a key inside dictionary that is inside a list that is inside a dictionary

This way works:

wjson['data']['current_condition'][0]['temp_C']
>> '10'
like image 8
HazimoRa3d Avatar answered Oct 10 '22 15:10

HazimoRa3d