Skip to content
Advertisement

Finding the headers for values in Json files

Hi I’ve got json data that looks something like this:

{
  "content": {
    "Header 1": [
      {
        "name": "Name1",

        }
      },
      {
        "name": "Name2",

        }
      }
    ],
    "Header 2": [
      {
        "name": "Name3",

        }
      }
    ],
  }
}

I’m looking to convert this into lists that look something like this:

header1 = ["Name1", "Name2"]
header2 = ["Name3"]

So far I’ve been able to get all the names that I want using objectpath.

from importlib.resources import path
import json
from unicodedata import name
import objectpath

path = (r"C:Userspathexample.json")

with open(path) as json_file:
    data = json.load(json_file)
    
tree_obj = objectpath.Tree(data)

names = list(tree_obj.execute('$..name'))

print (names)

But I’ve been unable to get the appropriate headers for each name as it appears to be nested under the ‘content’ header

Any help would be appreciated. Thanks :)

Advertisement

Answer

This does what you ask. Just iterate through the keys of “content”, and grab the keys in the subobjects.

import json

jsonx = """{
  "content": {
    "Header 1": [
      {
        "name": "Name1"
      },
      {
        "name": "Name2"
      }
    ],
    "Header 2": [
      {
        "name": "Name3"
      }
    ]
  }
}"""

data = json.loads(jsonx)
gather = {}

for k, v in data["content"].items():
    k1 = k.lower().replace(' ','')
    v1 = [vv['name'] for vv in v]
    gather[k1] = v1

print(gather)

Output:

{'header1': ['Name1', 'Name2'], 'header2': ['Name3']}

And for those who like one-liners:

gather = dict( 
    (k.lower().replace(' ',''),[vv['name'] for vv in v])
    for k, v in data["content"].items() )
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement