简体   繁体   中英

How can I loop over a specific key of a json object where we don't know how many nested objects have the same key?

I am having the following JSON structure:

{
    "name": "Birds",
    "title": "Birds",
    "default_language": "default",
    "id_string": "Birds",
    "type": "survey",
    "children": [
        {
            "type": "text",
            "name": "your_name",
            "label": "1. What is your name?"
        }
    ]
}

What I want to do is to add a new table that will have the following structure:

label                   name           

1. What is your name?   your_name

But sometimes, as my data shape is dynamic and there will be some inputs that has been deleted or new ones added, the JSON file will look like that:

{
  "name": "Birds",
  "title": "Birds",
  "default_language": "default",
  "id_string": "Birds",
  "type": "survey",
  "children": [
    {
      "type": "text",
      "name": "your_name",
      "label": "1. What is your name?",
      "children": [
        {
          "type": "text",
          "name": "your_favorite_food",
          "label": "2. What is your favorite food?",
          "children": [
            {
              "type": "text",
              "name": "your_favorite_drink",
              "label": "3. What is your favourite drink with it?"
            }
          ]
        }
      ]
    },
    {
      "children": [
        {
          "type": "text",
          "name": "your_country",
          "label": "Where are you coming from?"
        }
      ]
    }
  ]
}

In that case the table will look like something like that:

在此处输入图像描述

How can I loop over a specific key using Python if I don't know how many nested objects exists for this key?

You can find it recursively

d = {
  "name": "Birds",
  "title": "Birds",
  "default_language": "default",
  "id_string": "Birds",
  "type": "survey",
  "children": [
    {
      "type": "text",
      "name": "your_name",
      "label": "1. What is your name?",
      "children": [
        {
          "type": "text",
          "name": "your_favorite_food",
          "label": "2. What is your favorite food?",
          "children": [
            {
              "type": "text",
              "name": "your_favorite_drink",
              "label": "3. What is your favourite drink with it?"
            }
          ]
        }
      ]
    },
    {
      "children": [
        {
          "type": "text",
          "name": "your_country",
          "label": "Where are you coming from?"
        }
      ]
    }
  ]
}


def get_data(data, l, depth=0, prefix = ''):
    if(isinstance(data, dict)):
        depth += 1
        label = data.get('label')
        name = data.get('name')
        if(label and name):
            if(depth == 1):
                l.append((label, name))

            prefix = name if prefix=='' else prefix + '/' + name
                
        if('children' in data.keys()):

            get_data(data['children'], l, depth, prefix)
        else:

            l.append((label, prefix))
            
    elif(isinstance(data, list)):

        for each_data in data:
            get_data(each_data, l, depth,  prefix)

l = []
get_data(d['children'], l) #d is your actual data

output

[('1. What is your name?', 'your_name'),
 ('3. What is your favourite drink with it?',
  'your_name/your_favorite_food/your_favorite_drink'),
 ('Where are you coming from?', 'your_country')]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM