Mark K
Mark K

Reputation: 9378

To retrieve specific data from multiple similar portions in a JSON file

A part of JSON file's content as below:

{"ID": "PK45", "People": "Kate", "Date": "2020-01-05"}, {"ID": "OI85", "People": "John", "Date": "2020-01-18" }, {"ID": "CN658", "People": "Pevo", "Date": "2020-02-01" }

It has multiple portions containing "ID", "People" and "Date".

What I want to do is to retrieve John's ID (in this case "OI85").

If the key is unique, I can use:

data_content = json.loads(data)
ID = data_content['ID']

But there are multiple similar portions. So I can only locate the "John" first:

with open("C:\\the_file.json") as data_file:
    data = data_file.read()
    where = data.find('John')
    where_1 = data[where - 20 : where]
    ID = where_1[where_1.find('ID')+3:where_1.find('ID')+7]
    print (ID)

It looked clumsy.

What will be the smart way to retrieve the specific data from multiple similar portions in a JSON file?

Upvotes: 1

Views: 50

Answers (2)

Thierry Lathuille
Thierry Lathuille

Reputation: 24288

Iterate on the list of dicts until you find the right one:

import json

data = '[{"ID": "PK45", "People": "Kate", "Date": "2020-01-05"}, {"ID": "OI85", "People": "John", "Date": "2020-01-18" }, {"ID": "CN658", "People": "Pevo", "Date": "2020-02-01" }]'

data_content = json.loads(data)

def find_id_by_name(name, data):
    for d in data:
        if d['People'] == name:
            return d["ID"]
    else:
        raise ValueError('Name not found')

print(find_id_by_name('John', data_content))
# OI85

print(find_id_by_name('Jane', data_content))
# ... ValueError: Name not found

If you have to do many such searches, it may be worth creating another dict from your data to associate IDs to names:

ids_by_name = {d['People']:d['ID'] for d in data_content}
print(ids_by_name['John'])

# OI85

Upvotes: 1

michaeldel
michaeldel

Reputation: 2385

You probably should use the json module, which makes the task trivial:

import json

with open('data.json') as f:
    records = json.load(f)

for record in records:
    if record['People'] == 'John':
        print(record['ID'])
        break

Upvotes: 1

Related Questions