0

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?

1
  • So the 0 in your pprint is the index. Increment that number to get to the other json indexes Commented Jun 4, 2019 at 2:24

1 Answer 1

2

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"])
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, it's exactly what I needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.