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
© 2024 OneMinuteCode. All rights reserved.