Reputation: 13
I have to iterate through a json file. I imported into python as a list and print all the unique locality. However, the code I wrote only prints the first few localities and does not iterate through all 538 elements of the list:
import pandas as pd
import json
with open('data.json') as json_file:
json_file = json_file.readlines()
json_file = dict(map(json.loads, json_file))
for i in range (0, len(json_file)-1):
unique = json_file[i]['payload']['locality']
print(unique)
instead it still prints only about 30 localities, how can I fix this?
Heres a snippet of my file:
{ 'payload': {'existence_full': 1,
'geo_virtual': '["50.794876|-1.090893|20|within_50m|4"]',
'latitude': '50.794876',
'locality': 'Portsmouth',
'_records_touched': '{"crawl":16,"lssi":0,"polygon_centroid":0,"geocoder":0,"user_submission":0,"tdc":0,"gov":0}',
'email': '[email protected]',
'existence_ml': 0.9794948816203205,
'address': 'Winston Churchill Av',
'longitude': '-1.090893',
'domain_aggregate': '',
'name': 'University of Portsmouth',
'search_tags': ['The University of Portsmouth',
'The University of Portsmouth Students Union',
'University House'],
'admin_region': 'England',
'existence': 1,
'post_town': 'Portsmouth',
'category_labels': [['Community and Government',
'Education',
'Colleges and Universities']],
'region': 'Hampshire',
'review_count': '1',
'geocode_level': 'within_50m',
'tel': '023 9284 8484',
'placerank': 42,
'placerank_ml': 69.2774043602657,
'address_extended': 'Unit 4',
'category_ids_text_search': '',
'fax': '023 9284 3122',
'website': 'http://www.port.ac.uk',
'status': '1',
'neighborhood': ['The Waterfront'],
'geocode_confidence': '20',
'postcode': 'PO1 2UP',
'category_ids': [29],
'country': 'gb',
'_geocode_quality': '4'},
'uuid': '297fa2bf-7915-4252-9a55-96a0d44e358e'}
Upvotes: 1
Views: 714
Reputation: 96
You have not imported the data into a list, but into a dictionary. If you want to import the json into a list here is how you would do:
import json
with open('data.json') as json_file:
json_array = json.load(json_file)
for item in json_array:
unique = item['payload']['locality']
print(unique)
You said you wanted to print all the unique localities, but in your code you are printing all localities without checking if they are unique or not.
Upvotes: 1
Reputation: 17659
The issue may be that you have duplicate records in your file and when you load the data into a dict
the keys are colliding causing a portion of them to be dropped. Instead of storing the data in a dict
, store it in a list
instead:
import json
with open('data.json') as json_file:
lines = json_file.readlines()
records = list(map(json.loads, lines))
You should then be able to iterate through records
:
print('There are %d records' % len(records))
for record in records:
unique = record['payload']['locality']
print(unique)
Upvotes: 0