0

I have some data in JSON D3 format (specifically, flare.json). It looks like this,

json_data = {
   "name":"root",
   "children":[
      {
         "name":"435",
         "children":[
            {
               "name":"188",
               "children":[
                  {
                     "name":"198"
                  }
               ]
            },
            {
               "name":"436",
               "children":[
                  {
                     "name":"86"
                  },
                  {
                     "name":"115"
                  },
                  {
                     "name":"437"
                  }
               ]
            }

(etc.). The data has n levels. I have another nested list with "value" fields for some, but not all of the json_data "name" fields. For example,

[['198',50],['86',12],[...]]

I'd like to add corresponding "value" data to the json_data where applicable. So, the end result would look like this,

json_data_updates = {
   "name":"root",
   "children":[
      {
         "name":"435",
         "children":[
            {
               "name":"188",
               "children":[
                  {
                     "name":"198", "value":50
                  }
               ]
            },
            {
               "name":"436",
               "children":[
                  {
                     "name":"86","value":12
                  },
                  {
                     "name":"115"
                  },
                  {
                     "name":"437"
                  }
               ]
            }

I can use this to get the all of the name values in the json_data as a list

[root, 435, 188, 198, ...]

but am unsure how to then update the json_data to be in my desired format.

AMC
  • 2,642
  • 7
  • 13
  • 35
Kayla0x41
  • 105
  • 2
  • 4
  • 11
  • _but am unsure how to then update the json_data to be in my desired format._ Can you be more specific about what the issue is? Please see [ask], [help/on-topic]. – AMC May 07 '20 at 22:58

1 Answers1

1

It's not much help to recursively find names, because you'll need to then find them again to set value. Instead, modify the recursive function to set value when it's found.

#!/usr/bin/env python3

import json
import sys

json_data = {
   "name":"root",
   "children":[
      {
         "name":"435",
         "children":[
            {
               "name":"188",
               "children":[
                  {
                     "name":"198"
                  }
               ]
            },
            {
               "name":"436",
               "children":[
                  {
                     "name":"86"
                  },
                  {
                     "name":"115"
                  },
                  {
                     "name":"437"
                  }
               ]
            }
        ]
    }
]
}

values = [['198',50],['86',12]]

d = dict(values)

def set_names(root):
    try:
        value = d[root['name']]
    except KeyError:
        pass
    else:
        root['value'] = value

    for child in root.get('children', []):
        set_names(child)

set_names(json_data)
json.dump(json_data, sys.stdout)

Output:

{
  "name": "root",
  "children": [
    {
      "name": "435",
      "children": [
        {
          "name": "188",
          "children": [
            {
              "name": "198",
              "value": 50
            }
          ]
        },
        {
          "name": "436",
          "children": [
            {
              "name": "86",
              "value": 12
            },
            {
              "name": "115"
            },
            {
              "name": "437"
            }
          ]
        }
      ]
    }
  ]
}
chash
  • 3,975
  • 13
  • 29