Tristan Tristan - 6 months ago 10
JSON Question

Converting an "If Else" statement to a JSON file

I am having this as an

input
, let's call it
tree


if ( device_type_id <= 1 )
39 Clicks - 0.61%
2135 Conversions - 33.32%
else ( device_type_id > 1 )
if ( country_id <= 216 )
1097 Clicks - 17.12%
else ( country_id > 216 )
if ( browser_id <= 2 )
296 Clicks - 4.62%
else ( browser_id > 2 )
if ( browser_id <= 4 )
if ( browser_id <= 3 )
if ( operating_system_id <= 2 )
262 Clicks - 4.09%
1094 Impressions - 17.08%
else ( operating_system_id > 2 )
if ( operating_system_id <= 4 )
281 Clicks - 4.39%
220 Impressions - 3.43%
else ( operating_system_id > 4 )
if ( operating_system_id <= 6 )
4 Clicks - 0.06%
20 Impressions - 0.31%
else ( operating_system_id > 6 )
70 Impressions - 1.09%



else ( browser_id > 3 )
if ( operating_system_id <= 2 )
19 Clicks - 0.3%
21 Impressions - 0.33%
else ( operating_system_id > 2 )
19 Clicks - 0.3%
707 Impressions - 11.03%


else ( browser_id > 4 )
113 Clicks - 1.76%


Then I created this function using
tree
as an
input
:

def function_one(tree):
network = []
for line in tree.splitlines() :
if line.strip():
line = line.strip()
network.append(line)
else : break
if not line : break

res = []
res.append({'name':'Prediction Result', 'children':parser(network[1:])})
with open('static/json/structure_sklearn.json', 'w') as outfile:
json.dump(res, outfile)
return tree


As you may see, I used a
parser
function which is :

def parser(lines):
block = []
while lines :

if lines[0].startswith('if'):
bl = ' '.join(lines.pop(0).split()[1:]).replace('(', '').replace(')', '')
block.append({'name':bl, 'children':parser(lines)})

if lines[0].startswith('else'):
be = ' '.join(lines.pop(0).split()[1:]).replace('(', '').replace(')', '')
block.append({'name':be, 'children':parser(lines)})
elif not lines[0].startswith(('if','else')):
block2 = lines.pop(0)
block.append({'name':block2})
else:
break
return block


My issue is , I don't know in which phase I have missed something because the
json
file created from
function_one
is just :

[
{
"children":[
{
"name":"39 Clicks - 0.61%"
},
{
"name":"2135 Conversions - 33.32%"
}
],
"name":"Prediction Result"
}
]

Answer

Here is the modification I suggested:

def parser(lines):
    block = []
    while lines :

        if lines[0].startswith('if'):
            bl = ' '.join(lines.pop(0).split()[1:]).replace('(', '').replace(')', '')
            block.append({'name':bl, 'children':parser(lines)})

        elif lines[0].startswith('else'):
            be = ' '.join(lines.pop(0).split()[1:]).replace('(', '').replace(')', '')
            block.append({'name':be, 'children':parser(lines)})

        elif not lines[0].startswith(('if','else')):
            block2 = lines.pop(0)
            block.append({'name':block2})
        else:
            break   
    return block

With this change, I get the following JSON indented output:

[
  {
    "name": "Prediction Result", 
    "children": [
      {
        "name": "39 Clicks - 0.61%"
      }, 
      {
        "name": "2135 Conversions - 33.32%"
      }, 
      {
        "name": " device_type_id > 1 ", 
        "children": [
          {
            "name": " country_id <= 216 ", 
            "children": [
              {
                "name": "1097 Clicks - 17.12%"
              }, 
              {
                "name": " country_id > 216 ", 
                "children": [
                  {
                    "name": " browser_id <= 2 ", 
                    "children": [
                      {
                        "name": "296 Clicks - 4.62%"
                      }, 
                      {
                        "name": " browser_id > 2 ", 
                        "children": [
                          {
                            "name": " browser_id <= 4 ", 
                            "children": [
                              {
                                "name": " browser_id <= 3 ", 
                                "children": [
                                  {
                                    "name": " operating_system_id <= 2 ", 
                                    "children": [
                                      {
                                        "name": "262 Clicks - 4.09%"
                                      }, 
                                      {
                                        "name": "1094 Impressions - 17.08%"
                                      }, 
                                      {
                                        "name": " operating_system_id > 2 ", 
                                        "children": [
                                          {
                                            "name": " operating_system_id <= 4 ", 
                                            "children": [
                                              {
                                                "name": "281 Clicks - 4.39%"
                                              }, 
                                              {
                                                "name": "220 Impressions - 3.43%"
                                              }, 
                                              {
                                                "name": " operating_system_id > 4 ", 
                                                "children": [
                                                  {
                                                    "name": " operating_system_id <= 6 ", 
                                                    "children": [
                                                      {
                                                        "name": "4 Clicks - 0.06%"
                                                      }, 
                                                      {
                                                        "name": "20 Impressions - 0.31%"
                                                      }, 
                                                      {
                                                        "name": " operating_system_id > 6 ", 
                                                        "children": [
                                                          {
                                                            "name": "70 Impressions - 1.09%"
                                                          }
                                                        ]
                                                      }
                                                    ]
                                                  }
                                                ]
                                              }
                                            ]
                                          }
                                        ]
                                      }
                                    ]
                                  }
                                ]
                              }
                            ]
                          }
                        ]
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]

I can't tell if this is the actual form or output you are looking for, but it does apppear to include all the nodes from the original tree source.

Comments