Working with a JSON file that has nested structures and I want to extract attributes from the last level of the nested structure. Additionally, I want to compare these attributes with elements in an Excel row. Once we have the attributes, we can compare them with the Excel row elements.Excel data:
Here are some patterns from my json file:Service1:
"_comment": "Dict { Dict { list of Dicts [{ --> Value type = List or Integer","Services": {"RoadAttributes": {"egoInformationCompex": [ {"speedlimit": {"id": 5,"distanceStdDev": 5,"value": 5,"indexAdditionalSignList": [ 5, 5, 5 ],"source": 5,"unit": 1 },"trafficOnline": {"id": 5,"distanceStdDev": 5,"value": 5,"indexAdditionalSignList": [ 5, 5, 5 ],"source": 5,"unit": 1 },"averageSpeed": {"id": 5,"distanceStdDev": 5,"value": 5,"indexAdditionalSignList": [ 5, 5, 5 ],"source": 5,"unit": 1 },"slope": {"slope": 1.0,"distance": 5 },"messageCounter": 4 } ] } },
Service2:
"_comment": "Dict { Dict { list of list of Dicts [{ --> Value type = List or Integer or Dict","Services": {"RoadAttributesExtended": {"onlineInformation": [ {"localHazardList": [ {"id": 5,"distance": 5,"trafficIncidentID": 5,"combinedEventCode": 5,"localHazardType": 2 } ],"trafficOnlineList": [ {"id": 5,"distance": 5,"distanceStdDev": 5,"value": {"value1": 5,"value2": 5 },"indexAdditionalSignList": [ 5, 5, 5 ],"source": 5,"relevance": 5,"unit": 1 } ],"speedCourseListA": [ {"id": 5,"distance": 5,"distanceStdDev": 5,"speed": 5,"isGapStart": true,"confidence": 5 } ],"speedCourseListABucketType": 5,"speedCourseListAStatisticsType": 5,"speedCourseListB": [ {"id": 5,"distance": 5,"distanceStdDev": 5,"speed": 5,"isGapStart": true,"confidence": 5 } ],"speedCourseListBBucketType": 5,"speedCourseListBStatisticsType": 5,"acaEnableHandsFree": [ {"id": 5,"distance": 5,"value1": 5,"source": 5 } ],"roadObstacleList": [ {"id": 5,"distance": 5,"roadObstacleType": 5,"height": 5,"length": 5,"roadObstaclePosition": 5,"lane": 5,"crossingProbability": 5,"source": 5 } ],"roadCategoryList": [ {"id": 5,"distance": 5,"value": {"value1": 5,"value2": 5 },"source": 5 } ],"roadWavinessList": [ {"id": 5,"distance": 5,"value": {"value1": 5,"value2": 5 },"source": 5 } ],"messageCounter": 4 } ] } },
Here is python code I tried:
with open(r"ampmin.json") as f: data = js.load(f) for d in data: Temp_Data = d['Services'] if len (Temp_Data)!= 0: for s1_key, s1_val in Temp_Data.items(): # print(s1_key, type(s1_val)) for s2_key, s2_val in s1_val.items(): #print(s2_key, type(s2_val)) for s3_items in s2_val: #print('[0]', type(s3_items)) for s4_key, s4_val in s3_items.items(): print(s4_val)