简体   繁体   中英

How to append to the child node in JSON at every iteration using python provided new child node exists?

Sample

template = {
    "Table": [
        {
            "level": 2,
            "value": {
                "element Name": "",
                "add Row": "False",
                "cost Type": "",
                "flag": "flag",
                "cost": "",
                "actions": "True"
            },
            "RHS": {},
            "children": [
                {
                    "level": 3,
                    "value": {
                        "element Name": "",
                        "add Row": "False",
                        "cost Type": "",
                        "cost": "",
                        "actions": "True"
                    },
                    "RHS": {},
                    "children": []
                }
            ]
        }
    ]
}

Considering the above dictionary, I want to append to the last "children" and every time loop runs it should append to the children created in previous iteration. Loop 1: "children":{ "level": 4, "value": {"element Name": "", "add Row": "False", "cost Type": "", "cost": "", "actions": "True"}, "RHS": {}, "children": [] }


Loop 2:

iteration 2 "children":{ "level": 5, "value": {"element Name": "", "add Row": "False", "cost Type": "", "cost": "", "actions": "True"}, "RHS": {}, "children": [] }


and so on.

My code is:

Python code for loop

for _ in range(sublevels):
    number = number + 1
    child = {"level": sublevels + 2,
                     "value": {"element Name": "", "add Row": False,
                               "cost Type": "", "cost": "",
                               "actions": True}, "RHS": {}, "children": []}
    template['Table'][0]['children'].append(child)

Output: After iteration, the JSON should look like below

{
    "Table": [
        {
            "level": 2,
            "value": {
                "element Name": "",
                "add Row": "False",
                "cost Type": "",
                "flag": "flag",
                "cost": "",
                "actions": "True"
            },
            "RHS": {},
            "children": [
                {
                    "level": 3,
                    "value": {
                        "element Name": "",
                        "add Row": "False",
                        "cost Type": "",
                        "cost": "",
                        "actions": "True"
                    },
                    "RHS": {},
                    "children": [
                        [
                            {
                                "level": 4,
                                "value": {
                                    "element Name": "",
                                    "add Row": "False",
                                    "cost Type": "",
                                    "cost": "",
                                    "actions": "True"
                                },
                                "RHS": {},
                                "children": [
                                    [
                                        {
                                            "level": 5,
                                            "value": {
                                                "element Name": "",
                                                "add Row": "False",
                                                "cost Type": "",
                                                "cost": "",
                                                "actions": "True"
                                            },
                                            "RHS": {},
                                            "children": []
                                        }
                                    ]
                                ]
                            }
                        ]
                    ]
                }
            ]
        }
    ]
}

Iteration 1: template['Table'][0]['children']
Iteration 2: template['Table'][0]['children'][0]['children']
Iteration 3: template['Table'][0]['children'][0]['children'][0]['children']
import json

template = {"Table": []}
sublevels = 5

for _ in range(sublevels):
    #number = number + 1
    child = {"level": _ + 2,
                     "value": {"element Name": "", "add Row": False,
                               "cost Type": "", "cost": "",
                               "actions": True}, "RHS": {}, "children": []}

    cur_path = "[0]['children']"*_

    if _ == 0:
        template['Table'].append(child)
    else:
        exec(f"template['Table']{cur_path}.append(child)")


print(json.dumps(template, indent = 2))

Not the prettiest way, you should avoid using exec, but I was trying to call a JSON path from a dict, and it wasn't working so I used exec.

This works well and nests it tho..

Output I got from running this code:

{
  "Table": [
    {
      "level": 2,
      "value": {
        "element Name": "",
        "add Row": false,
        "cost Type": "",
        "cost": "",
        "actions": true
      },
      "RHS": {},
      "children": [
        {
          "level": 3,
          "value": {
            "element Name": "",
            "add Row": false,
            "cost Type": "",
            "cost": "",
            "actions": true
          },
          "RHS": {},
          "children": [
            {
              "level": 4,
              "value": {
                "element Name": "",
                "add Row": false,
                "cost Type": "",
                "cost": "",
                "actions": true
              },
              "RHS": {},
              "children": [
                {
                  "level": 5,
                  "value": {
                    "element Name": "",
                    "add Row": false,
                    "cost Type": "",
                    "cost": "",
                    "actions": true
                  },
                  "RHS": {},
                  "children": [
                    {
                      "level": 6,
                      "value": {
                        "element Name": "",
                        "add Row": false,
                        "cost Type": "",
                        "cost": "",
                        "actions": true
                      },
                      "RHS": {},
                      "children": []
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

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