Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get specific value from json object

I have a json file with the following structure:

"edition": 979,
  "attributes": [
    {
      "trait_type": "Background",
      "value": "Gray"
    },
    {
      "trait_type": "Base",
      "value": "Guy"
    },
    {
      "trait_type": "Body",
      "value": "FormalBlack"
    },
    {
      "trait_type": "Hand",
      "value": "None"
    },
    {
      "trait_type": "Head",
      "value": "Astronaut"
    },
    {
      "trait_type": "Pet",
      "value": "OrangeCat"
    }
  ],
  "properties": {
    "files": [
      {
        "uri": "979.png",
        "type": "image/png"
      }
    ],

How can I get get the data of ‘value’ in ‘attributes’?
I can get the whole ‘attributes’ object but can’t get to specific values inside, for example

f = open('./metadata/0.json')

xmp = json.load(f)

print (xmp['attributes'])

returns

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[{'trait_type': 'Background', 'value': 'Purple'}, {'trait_type': 'Base', 'value': 'RedGuy'}, {'trait_type': 'Body', 'value': 'Formal'}, {'trait_type': 'Hand', 'value': 'FlamingSword'}, {'trait_type': 'Head', 'value': 'RedHead'}, {'trait_type': 'Pet', 'value': 'None'}]

but I only need the value portion. I can run another method to search through it to get what I want but I imagine there is a much easier way.

>Solution :

To get all the values, you need to iterate on 'attributes' and take each 'value' value

xmp = {
    "edition": 979,
    "attributes": [
        {"trait_type": "Background","value": "Gray"},
        {"trait_type": "Base","value": "Guy"},
        {"trait_type": "Body","value": "FormalBlack"},
        {"trait_type": "Hand","value": "None"},
        {"trait_type": "Head","value": "Astronaut"},
        {"trait_type": "Pet","value": "OrangeCat"}
    ],
    "properties": {
      "files": [
        {"uri": "979.png","type": "image/png"}
      ]
    }
}


values = [item['value'] for item in xmp['attributes']]
# ['Gray', 'Guy', 'FormalBlack', ... ]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading