Skip to content

Instantly share code, notes, and snippets.

@sansyrox
Forked from amaciel81/parse_json.py
Created March 17, 2020 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sansyrox/92b513878669548be4c448f53476ac85 to your computer and use it in GitHub Desktop.
Save sansyrox/92b513878669548be4c448f53476ac85 to your computer and use it in GitHub Desktop.
Import a JSON file. If condition is met, print two attributes.
from json import load as json_load
with open("sample_input.json") as input_fh:
people = json_load(input_fh)
for person in people["people"]:
if person["age"] >= 30:
print("Name: {name}, City: {city}".format(name=person["name"], city=person["city"]))
{
"people":[
{
"name":"John",
"age":30,
"city":"Chicago"
},
{
"name":"Beth",
"age":40,
"city":"New York"
},
{
"name":"Lucy",
"age":25,
"city":"Los Angeles"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment