erkevarol
erkevarol

Reputation: 57

Python - appending JSON elements to a list

I have got a city.json which I gathered with parsing from a website and it is shown below

[
{"city": ["\r\nLondon\r\n"]},
{"city": ["\r\nEdinburgh\r\n"]},
{"city": ["\r\nBlackpool\r\n"]},
{"city": ["\r\nBrighton & Hove\r\n"]},
{"city": ["\r\nGlasgow\r\n"]},
{"city": ["\r\nManchester\r\n"]},
{"city": ["\r\nYork\r\n"]},
{"city": ["\r\nTorquay\r\n"]},
{"city": ["\r\nInverness\r\n"]},
{"city": ["\r\nLiverpool\r\n"]},
{"city": ["\r\nBirmingham\r\n"]},
{"city": ["\r\nBath\r\n"]},
{"city": ["\r\nScarborough\r\n"]},
{"city": ["\r\nCambridge\r\n"]},
{"city": ["\r\nNewquay\r\n"]},
{"city": ["\r\nAberdeen\r\n"]},
{"city": ["\r\nBelfast\r\n"]},
{"city": ["\r\nCardiff\r\n"]},
{"city": ["\r\nNewcastle upon Tyne\r\n"]},
{"city": ["\r\nBournemouth\r\n"]},
{"city": ["\r\nWhitby\r\n"]},
{"city": ["\r\nLlandudno\r\n"]},
{"city": ["\r\nOxford\r\n"]},
{"city": ["\r\nBristol\r\n"]},
{"city": ["\r\nLeeds\r\n"]}
]

I need to get each of the city and append it to my list. So far this is what I have done

import json

myList = []

with open('city.json') as json_data:
    data = json.load(json_data)
    for index in data:
       myList.append(index['city'])

for index in range(len(myList)):
   print (str(myList[index]).replace("[","").replace("]","").replace("\r\n",""))

I need my List only consists of [London, Edinburgh, Blackpool ... ] not any other characters like seen at the top. How can I solve this problem ?

Upvotes: 0

Views: 1199

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121654

Each value in each dictionary is a list containing one string. Just take the first element:

with open('city.json') as json_data:
    data = json.load(json_data)
    for index in data:
       myList.append(index['city'][0])  # note the indexing

You may want to use str.strip() to remove the \r\n whitespace around each city value:

with open('city.json') as json_data:
    data = json.load(json_data)
    for index in data:
       myList.append(index['city'][0].strip())

You can put the whole thing into a list comprehension, no need to use list.append():

with open('city.json') as json_data:
    data = json.load(json_data)
    myList = [d['city'][0].strip() for d in data]

Demo, putting your JSON sample into the string json_data:

>>> data = json.loads(json_data)
>>> [d['city'][0].strip() for d in data]
['London', 'Edinburgh', 'Blackpool', 'Brighton & Hove', 'Glasgow', 'Manchester', 'York', 'Torquay', 'Inverness', 'Liverpool', 'Birmingham', 'Bath', 'Scarborough', 'Cambridge', 'Newquay', 'Aberdeen', 'Belfast', 'Cardiff', 'Newcastle upon Tyne', 'Bournemouth', 'Whitby', 'Llandudno', 'Oxford', 'Bristol', 'Leeds']
>>> from pprint import pprint
>>> pprint(_)
['London',
 'Edinburgh',
 'Blackpool',
 'Brighton & Hove',
 'Glasgow',
 'Manchester',
 'York',
 'Torquay',
 'Inverness',
 'Liverpool',
 'Birmingham',
 'Bath',
 'Scarborough',
 'Cambridge',
 'Newquay',
 'Aberdeen',
 'Belfast',
 'Cardiff',
 'Newcastle upon Tyne',
 'Bournemouth',
 'Whitby',
 'Llandudno',
 'Oxford',
 'Bristol',
 'Leeds']

Upvotes: 4

Related Questions