Home > Software design >  Getting index of value in nested JSON dictionary to retrieve sibling values in Python
Getting index of value in nested JSON dictionary to retrieve sibling values in Python

Time:05-27

I started by converting XML to JSON.

I have a nested value I would like to find the index of in order to retrieve it's sibling dictionary. These elements are under the same parent Location. The JSON below is abridged, there are many more Location dictionaries.

For example, from providing a value of New York to:
Location > LocationMetaData > LocationName

I'd like to retrieve:
Location > LocationData > TimeData > Period > DateTimeTo and
Location > LocationData > TimeData > Element > ElementIndex

I'm stuck here, trying something along the lines of:

json_d['Root']['Location'][0]['LocationMetaData']['LocationName']

JSON

{
  "Root":{
    "Origin":{
      "Firstname":"Guy",
      "Lastname":"Man"
    },
    "Identification":{
      "Title":"The Title",
      "DateTime":"2022-05-26 09:00"
    },
    "Location":[
      {
        "LocationMetaData":{
          "LocationId":"192",
          "LocationName":"New York"
        },
        "LocationData":{
          "TimeData":[
            {
              "Period":{
                "DateTimeFrom":"2022-05-26 12:00",
                "DateTimeTo":"2022-05-26 13:00"
              },
              "Element":{
                "ElementValue":"V",
                "ElementIndex":"9"
              }
            },
            {
              "Period":{
                "DateTimeFrom":"2022-05-26 13:00",
                "DateTimeTo":"2022-05-26 14:00"
              },
              "Element":{
                "ElementValue":"V",
                "ElementIndex":"8"
              }
            },
            {
              "Period":{
                "DateTimeFrom":"2022-05-26 14:00",
                "DateTimeTo":"2022-05-26 15:00"
              },
              "Element":{
                "ElementValue":"H",
                "ElementIndex":"6"
              }
            }
          ]
        }
      }
    ],
    "Location":[
      {
        "LocationMetaData":{
          "LocationId":"168",
          "LocationName":"Chicago"
        },
        "LocationData":{
          "TimeData":[
            {
              "Period":{
                "DateTimeFrom":"2022-05-26 12:00",
                "DateTimeTo":"2022-05-26 13:00"
              },
              "Element":{
                "ElementValue":"V",
                "ElementIndex":"9"
              }
            },
            {
              "Period":{
                "DateTimeFrom":"2022-05-26 13:00",
                "DateTimeTo":"2022-05-26 14:00"
              },
              "Element":{
                "ElementValue":"V",
                "ElementIndex":"8"
              }
            },
            {
              "Period":{
                "DateTimeFrom":"2022-05-26 14:00",
                "DateTimeTo":"2022-05-26 15:00"
              },
              "Element":{
                "ElementValue":"H",
                "ElementIndex":"6"
              }
            }
          ]
        }
      }
    ]
  }
}

Python

#!/usr/bin/env python3

import xmltodict
import json
import requests

url = "https://example.com/file.xml"
xml = requests.get(url)

json_s = json.dumps(xmltodict.parse(xml.content), indent=2)
#print(json_s)

#dict
json_d = json.loads(json_s)

#need to find index by providing value of LocationName
json_d = json_d['Root']['Location'][0]
city = json_d['LocationMetaData']['LocationName'] #new york

count = 0

for key in json_d['LocationData']['TimeData']:
    timeto = key['Root']['DateTimeTo'] 
    indx = key['Element']['ElementIndex']
    level = key['Element']['ElementValue']
    level = {
        'L': 'Low',
        'M': 'Medium',
        'H': 'High',
        'V': 'Very High',
        'E': 'Extreme'} [level]
    print(f'{timeto}\t{indx} ({level})')
    count  = 1
    if count == 15: break

CodePudding user response:

If json_d is your parsed json file, you can try:

for location in json_d["Root"]["Location"]:
    city = location["LocationMetaData"]["LocationName"]
    date_time_to = []
    element_idx = []
    element_values = []
    for td in location["LocationData"]["TimeData"]:
        date_time_to.append(td["Period"]["DateTimeTo"])
        element_idx.append(td["Element"]["ElementIndex"])
        element_values.append(td["Element"]["ElementValue"])

    print(city)
    print("-" * 80)
    for t, e, v in zip(date_time_to, element_idx, element_values):
        print(f"{t} - {e} - {v}")
    print()

Prints:

Chicago
--------------------------------------------------------------------------------
2022-05-26 13:00 - 9 - V
2022-05-26 14:00 - 8 - V
2022-05-26 15:00 - 6 - H
  • Related