ThatOnePythonNoob ThatOnePythonNoob - 2 months ago 7
JSON Question

Extract a specific section of a string depending on an input

I have a very large

.json
converted into a
string
, containing numerous cities/countries.

I'd like to extract the information of the city depending on the user's choice of country (London is just an example).

For example, if the
Country
the user inputed was:
UK
, the following information would be extracted from the string:

I'm not too sure on how I could possibly achieve this due to my inexperience, but am aware that it would require an if statement. My progress so far:

Country = raw_input('Country: ')
if 'UK' in string:
???

Answer

The initial response wasn't great because a few of us overlooked the raw JSON. However, you did provide it so in future it would be better to make it more obvious that the snippet you showed had a more complete (and valid) counterpart.

That said, I would load the data into a dictionary and do something like:

import json

json_string = """{
  "response": {
  "version":"0.1",
  "termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
  "features": {
  "conditions": 1
  }
        , "results": [
        {
        "name": "London",
        "city": "London",
        "state": "AR",
        "country": "US",
        "country_iso3166":"US",
        "country_name":"USA",
        "zmw": "72847.1.99999",
        "l": "/q/zmw:72847.1.99999"
        }
        ,
        {
        "name": "London",
        "city": "London",
        "state": "KY",
        "country": "US",
        "country_iso3166":"US",
        "country_name":"USA",
        "zmw": "40741.1.99999",
        "l": "/q/zmw:40741.1.99999"
        }
        ,
        {
        "name": "London",
        "city": "London",
        "state": "MN",
        "country": "US",
        "country_iso3166":"US",
        "country_name":"USA",
        "zmw": "56036.3.99999",
        "l": "/q/zmw:56036.3.99999"
        }
        ,
        {
        "name": "London",
        "city": "London",
        "state": "OH",
        "country": "US",
        "country_iso3166":"US",
        "country_name":"USA",
        "zmw": "43140.1.99999",
        "l": "/q/zmw:43140.1.99999"
        }
        ,
        {
        "name": "London",
        "city": "London",
        "state": "ON",
        "country": "CA",
        "country_iso3166":"CA",
        "country_name":"Canada",
        "zmw": "00000.1.71623",
        "l": "/q/zmw:00000.1.71623"
        }
        ,
        {
        "name": "London",
        "city": "London",
        "state": "TX",
        "country": "US",
        "country_iso3166":"US",
        "country_name":"USA",
        "zmw": "76854.1.99999",
        "l": "/q/zmw:76854.1.99999"
        }
        ,
        {
        "name": "London",
        "city": "London",
        "state": "",
        "country": "UK",
        "country_iso3166":"GB",
        "country_name":"United Kingdom",
        "zmw": "00000.1.03772",
        "l": "/q/zmw:00000.1.03772"
        }
        ,
        {
        "name": "London",
        "city": "London",
        "state": "WV",
        "country": "US",
        "country_iso3166":"US",
        "country_name":"USA",
        "zmw": "25126.1.99999",
        "l": "/q/zmw:25126.1.99999"
        }
        ]
    }
}"""

json_object = json.loads(json_string)

world_dict = {}
for item in json_object['response']['results']:
    item_country = item['country']
    in_dict = world_dict.get(item_country)
    if in_dict:
        world_dict[item_country].extend([item])
    else:
        world_dict[item_country] = [item]

country = raw_input('Country: ')

response = world_dict.get(country)
if response:
    for item in response:
        print item
else:
    print "Not a valid country"

EDIT: Based on comment to use URL rather than a JSON string.

import requests

url = 'http://api.wunderground.com/api/a8c3e5ce8970ae66/conditions/q/London.json'

data = requests.get(url).json()


world_dict = {}
for item in data['response']['results']:
    item_country = item['country']
    in_dict = world_dict.get(item_country)
    if in_dict:
        world_dict[item_country].extend([item])
    else:
        world_dict[item_country] = [item]

country = raw_input('Country: ')

response = world_dict.get(country)
if response:
    for item in response:
        print item
else:
    print "Not a valid country"
Comments