How to extract specific fields and values from a JSON with python?

12,786

filtered_data is an ordinary list, so you can access individual dictionaries from it using ordinary indexing or iteration. You can take each element and put them into a new dictionary, keyed by user name:

filtered_data = [{'id': 1021972, 'Aging_Deferred_Transferred': '', 'Aging_Open_Issue': '0.94', 
'Aging_Un_investigated_Issue': '0.94', 'User': 'John P.', 'ModifiedOn': '2017-09-04 21:29:59', 
'Open_date':'2017-08-04 01:34:18','End_date':'2017-09-05 00:29:01',
'Ticket_status':'Transferred'},{'id': 1036722, 'Aging_Deferred_Transferred': '', 'Aging_Open_Issue': '0.12', 
'Aging_Un_investigated_Issue': '0.01', 'User': 'John P.', 'ModifiedOn': '2017-09-04 21:29:59', 
'Open_date':'2017-09-01 00:34:18','End_date':'',
'Ticket_status':'Researching'},{'id': 1015621, 'Aging_Deferred_Transferred': '', 'Aging_Open_Issue': '0.99', 
'Aging_Un_investigated_Issue': '0.11', 'User': 'John D.', 'ModifiedOn': '2017-06-05 12:19:59', 
'Open_date':'2017-01-01 00:00:18','End_date':'2017-09-01 20:20:57',
'Ticket_status':'Closed'}]

data_by_user = {}
for d in filtered_data:
    data_by_user[d["User"]] = d

print(data_by_user["John P."])

Now you can access John P's data (or anyone else's data) by indexing the new dictionary with their name.


Edit: you can be selective about which key/value pairs will be in the new dictionary, by constructing new dicts that explicitly only select from the keys you specify:

data_by_user = {}
for d in filtered_data:
    data_by_user[d["User"]] = {k:d[k] for k in ("id", "Open_date", "User", "Ticket_status", "End_date")}
Share:
12,786
abautista
Author by

abautista

Connecting the dots with my tech skills.

Updated on June 05, 2022

Comments

  • abautista
    abautista almost 2 years

    I am iterating a JSON and I want to extract the following fields from this object:

    1. Id,
    2. Open_date
    3. User
    4. Ticket_status
    5. End_date

    The data structure that I have is like the following :

    filtered_data = 
    [{'id': 1021972, 'Aging_Deferred_Transferred': '', 'Aging_Open_Issue': '0.94', 
    'Aging_Un_investigated_Issue': '0.94', 'User': 'John P.', 'ModifiedOn': '2017-09-04 21:29:59', 
    'Open_date':'2017-08-04 01:34:18','End_date':'2017-09-05 00:29:01',
    'Ticket_status':'Transferred'},{'id': 1036722, 'Aging_Deferred_Transferred': '', 'Aging_Open_Issue': '0.12', 
    'Aging_Un_investigated_Issue': '0.01', 'User': 'John P.', 'ModifiedOn': '2017-09-04 21:29:59', 
    'Open_date':'2017-09-01 00:34:18','End_date':'',
    'Ticket_status':'Researching'},{'id': 1015621, 'Aging_Deferred_Transferred': '', 'Aging_Open_Issue': '0.99', 
    'Aging_Un_investigated_Issue': '0.11', 'User': 'John D.', 'ModifiedOn': '2017-06-05 12:19:59', 
    'Open_date':'2017-01-01 00:00:18','End_date':'2017-09-01 20:20:57',
    'Ticket_status':'Closed'}]
    

    I want to return a data structure such as a Dictionary (if you think there is a better a data structure for achieving my goal please mention it) as the following:

    dict_user_john_p = {'ID':1021972,'Open_date':'2017-08-04 01:34:18','Ticket_status':'Transferred','End_date':'2017-09-05 00:29:01',
    'ID':1036722,'Open_date':'2017-09-01 00:34:18','Ticket_status':'Researching','End_date':''}
    
    dict_user_john_D = {'ID':1015621,...,'End_date':'1015621'}
    

    How can I extract the mentioned specific fields and return them as another data structure (dictionary)?