简体   繁体   English

在我们不知道有多少嵌套对象具有相同键的情况下,如何遍历 json object 的特定键?

[英]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:我有以下 JSON 结构:

{
    "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:但有时,由于我的数据形状是动态的,并且会删除一些输入或添加新的输入,JSON 文件将如下所示:

{
  "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?如果我不知道该键存在多少嵌套对象,如何使用 Python 遍历特定键?

You can find it recursively你可以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 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')]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在 my_dictionary 中找到带有字典的键,但我不知道 my_dictionary 中有多少个字典? - How may i find the key in my_dictionary , with dictionaries, and i don`t know exactly how many dictionaries i will have in my_dictionary? 如何检索不包含特定键的 JSON 的最后一个子项 - How to retrieve the last child of a JSON that don't contain a specific key I don't know how to select specific items from json object in my web application using flask - I don't know how to select specific items from json object in my web application using flask 我如何追加/合并/配对相同索引或键的值,而忽略没有匹配索引或键的值? - How would I append/merge/pair values of the same index or key and ignore values that don't have a matching index or key? 如何以必须从同一字符串中提取键和值的方式将字符串转换为字典 - How can I convert a string to dictionary in a manner where I have to extract key and value from same string 如果在大 JSON 数据中发现类似的特定键,我该如何删除嵌套数据? - How can I remove a nested data If a specific key found similar in big JSON data? 如何递归查找嵌套 JSON 中的特定键? - How to recursively find specific key in nested JSON? 我可以为未知数量的按钮列表创建破折号回调吗? 即不知道列表将有多少按钮以及需要多少回调 - Can I create dash callbacks for a list of unknown number of buttons. I.e don't know how many buttons the list will have and how many callbacks needed 我不知道如何从嵌套字典中提取特定的键:值对 - I can't figure out how to extract specific key:value pairs from a nested dictionary 如何在Python中为嵌套JSON对象重命名键 - How to rename key for nested JSON object in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM