Reputation: 138
I'm trying to figure out a way to display only the name and id from each object. I checked a lot of answers here already but I'm new to Python and I don't really know how to do this. This is my JSON file:
{"tags": [
{
"tagGroup": "Named Tags",
"lastConfig": null,
"notes": null,
"color": "#ffcc33ff",
"name": "tag 3",
"lastConfigTS": null,
"fwVersion": null,
"id": "a4da22e0296b"
},
{
"tagGroup": "Named Tags",
"lastConfig": null,
"notes": null,
"color": "#ff00ccdd",
"name": "tag 4",
"lastConfigTS": null,
"fwVersion": null,
"id": "a4da22e04235"
},
{
"tagGroup": "Named Tags",
"lastConfig": null,
"notes": null,
"color": "#ff00cccc",
"name": "tag 5",
"lastConfigTS": null,
"fwVersion": null,
"id": "a4da22e02225"
}
]}
This is my current code:
import json
from pprint import pprint
with open('tags.json') as f:
data = json.load(f)
pprint(data["tags"][0]["id"])
I can get it to print only the first id, but I don't really know where to go from here. Is there a way to print only all of name and id values?
Upvotes: 0
Views: 103
Reputation: 1345
Simply iterating through the list of dictionaries should do the trick for you.
import json
from pprint import pprint
with open('tags.json') as f:
data = json.load(f)
for tag in data["tags"]:
pprint(tag["id"])
pprint(tag["name"])
Upvotes: 2