Thrillofit86
Thrillofit86

Reputation: 629

How to scrape attributes from json values

I am trying to scrape some values through a json that looks like:

{
   "attributes":{
      "531":{
         "id":"531",
         "code":"taille",
         "label":"taille",
         "options":[
            {
               "id":"30",
               "label":"40",
               "is_in":"0"
            },
            {
               "id":"31",
               "label":"41",
               "is_in":"1"
            }
         ]
      }
   },
   "template":"Helloworld"
}

My issue is that the number 531 is different in each json file that I am trying to scrape and what I am trying to grab through this json is the label and is_in value

What I have done so far is that I tried to do something like this but I am stuck and dont know how to do if the 531 is changing to something else

getOption = '{
       "attributes":{
          "531":{
             "id":"531",
             "code":"taille",
             "label":"taille",
             "options":[
                {
                   "id":"30",
                   "label":"40",
                   "is_in":"0"
                },
                {
                   "id":"31",
                   "label":"41",
                   "is_in":"1"
                }
             ]
          }
       },
       "template":"Helloworld"
    }'

for att, values in getOption.items():
    print(values)

So how do I possible scrape the value label and is_in?

Upvotes: 0

Views: 53

Answers (1)

Fredrik
Fredrik

Reputation: 451

I'm not sure if you can have several 531 keys but you can loop through them.

getOption = {
    "attributes":{
        "531":{
            "id":"531",
            "code":"taille",
            "label":"taille",
            "options":[
            {
                "id":"30",
                "label":"40",
                "is_in":"0"
            },
            {
                "id":"31",
                "label":"41",
                "is_in":"1"
            }
            ]
        }
    },
    "template":"Helloworld"
}

attributes = getOption['attributes']
for key in attributes.keys():
    for item in attributes[key]['options']:
        print(item['label'], item['is_in'])

Upvotes: 1

Related Questions