Selecting fields from JSON output
Assume you stored that dictionary in a variable called values. To get id
in to a variable, do:
idValue = values['criteria'][0]['id']
If that json is in a file, do the following to load it:
import json
jsonFile = open('your_filename.json', 'r')
values = json.load(jsonFile)
jsonFile.close()
If that json is from a URL, do the following to load it:
import urllib, json
f = urllib.urlopen("http://domain/path/jsonPage")
values = json.load(f)
f.close()
To print ALL of the criteria, you could:
for criteria in values['criteria']:
for key, value in criteria.iteritems():
print key, 'is:', value
print ''
Getting specific field values from Json Python
You want to print the _id
of each element of your json list, so let's do it by simply iterating over the elements:
input_file = open('input_file.txt')
data = json.load(input_file) # get the data list
for element in data: # iterate on each element of the list
# element is a dict
id = element['_id'] # get the id
print(id) # print it
If you want to transform the list of elements into a list of ids for later use, you can use list comprehension:
ids = [ e['_id'] for e in data ] # get id from each element and create a list of them
How to extract specific fields and values from a JSON with python?
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")}
get exact value of elements from json body
Since, data
is already a dictionary object, you don't need json
and can simply use list comprehension as:
apps = [elt["fields"]["Application"] for elt in data] # Output ['1', '2']
You could also use get()
in case the keys
are missing for some elements in the list:
apps = [elt.get("fields",{}).get("Application") for elt in data] # Output ['1', '2']
Related Topics
Generate List of Quarters Betweeen Given Dates
How to Export a Table Dataframe in Pyspark to Csv
Python-Compare Two String Columns in Same Dataframe, Return Matching Result
How to Find Consecutive Numbers in a Python List
In Python, How to Check If a Date Is Valid
How to Tell Python to Convert Integers into Words
Convert String from Big-Endian to Little-Endian or Vice Versa in Python
Plot Line Graph from Pandas Dataframe (With Multiple Lines)
Dice Rolling Simulator in Python
Asking the User for Input Until They Give a Valid Response
How to Clear All Widgets from a Tkinter Window in One Go Without Referencing Them All Directly
How to Convert List into String With Quotes in Python
Iterate Over Worksheets, Rows, Columns
Python Check Multi-Level Dict Key Existence
Combine Year, Month and Day in Python to Create a Date
How to Write 2 Lists of Items in 2 Columns Instead of 2 Arrays
I Want to Multiply Two Columns in a Pandas Dataframe and Add the Result into a New Column