1

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']
3
  • If you are always working with template['Table'][0] then what is the point of having Table as a list? Also, is your question how to append an item to the 'children' list or how to do it recursively(ish) for each sublevel? Commented Feb 23, 2021 at 13:37
  • I am working on a huge json creation and need to append to template['Table'][0]['children'] in iteration 1 and then to template['Table'][0]['children'][0]['children'] and so on. Commented Feb 23, 2021 at 13:53
  • So, you're looking to change the content of the deepest children node of an object. Please look at stackoverflow.com/questions/30928331/… to see if the solution could be customized to your need (the post looks for the deepest node, you would need to change it to find the deepest node with a particular name). Commented Feb 23, 2021 at 14:18

1 Answer 1

1
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": []
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.