-2
{
    "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.

  • 2
    The order isn't lost, it was never there. JSON objects are not guaranteed ordered; if you care about order, use an array, or sort on appropriate keys when iterating. – jonrsharpe Mar 04 '17 at 23:18
  • 2
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ](http://stackoverflow.com/tour) and [How to Ask](http://stackoverflow.com/questions/how-to-ask). – TigerhawkT3 Mar 04 '17 at 23:19
  • The above json is representing an Abstract Syntax Tree, It would be very helpful if there was a way to extract the info the way I have mentioned above..thanks ! – Arun Kumar Mar 04 '17 at 23:21
  • Ok will post screenshots in a minute. – Arun Kumar Mar 04 '17 at 23:22
  • 1
    Please do not post screenshots. Code is text, write it as such in the question. The same goes for tracebacks and the like; search cannot index the contents of images. – Ilja Everilä Mar 04 '17 at 23:23
  • I just updated with outputs so far.. – Arun Kumar Mar 04 '17 at 23:39
  • Use the `{}` button in the editor (with a block of code selected) to format it as such. – Charles Duffy Mar 04 '17 at 23:45
  • BTW, your structure has a bunch of lists in its structure -- while ordering of keys in a dict is not retained, order of dicts in a list certainly is, and if this JSON format has been appropriately constructed for your use case, then lists should be used everywhere order is important. – Charles Duffy Mar 04 '17 at 23:47
  • thanks @CharlesDuffy. I updated with all the suggestions. The problem is i have never worked with json and also none of the json questions on here seem to have such a nested structure. Yea as you were saying once i use an interator to which converts it into a dict then i print it , it just spits out list in each line and order is gone. Thus a code snippet would be very helpful and it does not have to use iterators, I used it because i came across elsewhere.. – Arun Kumar Mar 04 '17 at 23:57
  • If you have never worked with JSON, you will have to learn about it first. SO isn't a free coding service. – TigerhawkT3 Mar 05 '17 at 00:04
  • Completely agree @TigerhawkT3 & this is not a homework I want to tell you that. This is my first Data analytics research project I am working on and I am an amateur coder, before asking here I have worked on the same thing for 5 days now straight. I could show you all the screenshots of outputs as a user suggested I can't upload them here. Hence I asked for help. Appreciate quick responses and this is my 1st time asking a question on this website.. Any comments on further approach will be very helpful. I have written recursion before but may be I am just intimidated never having done JSON.. – Arun Kumar Mar 05 '17 at 00:12
  • JSON is no different from any other nested structure consisting of basic types (integers, lists, dicts, strings), except for having a bunch of silly limitations inherited from JavaScript. – Charles Duffy Mar 05 '17 at 00:12

1 Answers1

1

The most obvious implementation is as a recursive function:

>>> def process(data):
...     if 'type' in data: print data['type']
...     if 'children' in data:
...         for child in data['children']:
...             process(child)
...
>>> j = json.load(open('test.json', 'r'))
>>> process(j)
program
maze_forever
DO
maze_ifElse
isPathForward
DO
maze_moveForward
ELSE
maze_turn
turnLeft

Note that we're printing the current structure's type before we recurse into children.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • I am new to python and moreover I have been just coding only twice a day in java over the past 6 months. This is actually for my very first data science project & not my homework. Your elegant solution was so helpful & honestly i am embarrassed about my recursion skills. Without using any fancy packages the order is being perfectly restored & that is what i wanted & I was thinking about preorder, DFS etc but i knew recursion was it but i am very poor in python syntax your if condition in second line of code made me realize that !!! learnt couple of things today Thanks so much ! – Arun Kumar Mar 05 '17 at 01:16