{
"id": "9",
"children": [{
"id": "8",
"children": [{
"id": "7",
"children": [{
"id": "6",
"children": [
{
"id": "0",
"type": "isPathForward"
},
{
"id": "2",
"children": [{
"id": "1",
"type": "maze_moveForward"
}],
"type": "DO"
},
{
"id": "5",
"children": [{
"id": "4",
"children": [{
"id": "3",
"type": "turnLeft"
}],
"type": "maze_turn"
}],
"type": "ELSE"
}
],
"type": "maze_ifElse"
}],
"type": "DO"
}],
"type": "maze_forever"
}],
"type": "program"
}
The above valid JSON is basically an AST (Abstract Syntax Tree) and I want to extract only the "type" in the order : 1) node followed by 2) Left child and then 3) Right child
Exactly like below for the above JSON :
Program
maze_forever
DO
maze_ifElse
isPathforward
Do
maze_moveForward
Else
maze_turn
turn_Left
I have not worked with json I tried using generators in python but the order is lost in the process as it converts to a dict.
Could you write a python implementation for this ?
UPDATE !!!!
So far i have tried :
import json json_string= json.loads(above json)
when i type :
for i in json_string:
... print(i)
...
OUTPUT
type
id
children
I also tried
import pandas as pd
d=pd.read_json('{ "id": "9", "children": [{ "id": "8", "children": [{ "id": "7", "children": [{ "id": "6", "children": [ { "id": "0", "type": "isPathForward" }, { "id": "2", "children": [{ "id": "1", "type": "maze_moveForward" }], "type": "DO" }, { "id": "5", "children": [{ "id": "4", "children": [{ "id": "3", "type": "turnLeft" }], "type": "maze_turn" }], "type": "ELSE" } ], "type": "maze_ifElse" }], "type": "DO" }], "type": "maze_forever" }], "type": "program"}')
>>> d
output :
children id type
0 {'type': 'maze_forever', 'id': '8', 'children'... 9 program
Both cases above :
I do not know how to recursively get inside children as every child has one or more children inside. Most answers I searched donot explain for a JSON that is as nested as my JSON above.