3

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)?

abautista
  • 2,410
  • 5
  • 41
  • 72
  • 6
    Tip: there is no such thing as "a JSON". Your data structure is an ordinary list containing ordinary dictionaries. You can access their data the same way you access the data of any other list or dictionary. – Kevin Sep 05 '17 at 19:06
  • but then how can I get each key and value of the dictionaries and arrange them as one? – abautista Sep 05 '17 at 19:13
  • As for having a lot of individual dictionaries whose name only differs by user name, see Tigerhawk's answer at https://stackoverflow.com/a/38972761/953482. The TLDR is: "don't do that. Instead, store all of your values in a dictionary" – Kevin Sep 05 '17 at 19:18

1 Answers1

3

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")}
Kevin
  • 74,910
  • 12
  • 133
  • 166
  • One observation: I've got all the data for John P. but I got all the keys and values. Isn't it possible to get only the keys and values ID, open_date, user, ticket_status, end_date? – abautista Sep 05 '17 at 19:31
  • 1
    There are a number of ways to do that, but the simplest is probably to just iterate through the keys you want, and add only them. Edited. – Kevin Sep 05 '17 at 19:35