raffzwo
raffzwo

Reputation: 53

How to get values from keys from JSON

I'm trying to get some values from a JSON file in Python to create a curl command with these values from the file.

The values that i am looking for are "alarm-id" and "sequence-id".

The JSON file looks like this:

{
  "data": [
    {
      "attributes": {
        "alarm-id": "3672400101833445418",
        "sequence-id": "1573135238000"
      }
    }
  ]
}

I've tried get() but i cant figure out how to use this correctly.

If you need more information just ask.

Thanks in advance!

Best regards

Upvotes: 3

Views: 204

Answers (2)

dataaldis
dataaldis

Reputation: 51

You have combination of dictionaries and list. Access dictionary key by name dict["key"] and list element by index.

In short, like this:

>>> d = {"data": [{"attributes":{"alarm-id": "3672400101833445418","sequence-id": "1573135238000"}}]}
>>> d["data"][0]["attributes"]["alarm-id"]
'3672400101833445418'

Upvotes: 1

Gamopo
Gamopo

Reputation: 1598

This should work for you

import json

data = json.loads(#yourJSON)
for attribute in data['data']:
    print(attribute['attributes']['alarm-id'])
    print(attribute['attributes']['sequence-id'])

Upvotes: 3

Related Questions