I'm trying to implement it like this using Python3 recursive function, but it's blocked, so I'm asking you a question!

Asked 1 years ago, Updated 1 years ago, 401 views

It is a format with a netsted structure as shown below.

example

{
  "a": {
    "a_a": [
      {
        "a_a_a": [
          {
            "a_a_a_a": {
              "a_a_a_a_a": {
                "value": "first value",
                "check": true
              }
            }
          },
          {
            "a_a_a_b": {
              "a_a_a_b_a": {
                "value": "second value",
                "check": true
              }
            }
          }
        ]
      }
    ]
  }
}

result (in the desired direction)

[
    {"id": 1, "field_id": "a | 0",  "parent_id": None, "value": None, "check": False},
    {"id": 2, "field_id": "a_a | 0", "parent_id": 1, "value": None, "check": False},
    {"id": 3, "field_id": "a_a_a | 0", "parent_id": 2, "value": None, "check": False},
    {"id": 4, "field_id": "a_a_a_a | 0", "parent_id": 3, "value": None, "check": False},
    {"id": 5, "field_id": "a_a_a_a_a | 0", "parent_id": 4, "value": "first_value", "check": True},
    {"id": 6, "field_id": "a_a_a | 1", "parent_id": 2, "value": None, "check": False},
    {"id": 7, "field_id": "a_a_a_b | 0", "parent_id": 6, "value": None, "check": False},
    {"id": 8, "field_id": "a_a_a_b_a | 0", "parent_id": 7, "value": "second value", "check": True},
]

Attempted Direction

def recursive(request: dict):
    serialize = []
    for k, v in request.item():
        if isinstance(v, list):
            for re_list in v:
                recursive(re_list)
        elif isinstance(v, dict):
            recursive(v)
        else:
            serialize.extend(
                id.get("id"),
                f"{request.get('field_id')} | | cnt",
                request.get("parent_id", None),
                request.get("check", False)
            )
    return serialize

python3 algorithm recursive

2022-10-11 01:00

1 Answers


2022-10-11 01:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.