0

I have a dictionary created from JSON. I would like to access items in the dictionairy through arrays containing their keys. Visualised JSON:

{
  "name": "Chiel",
  "industry": {
    "IndustryName": "Computer Science",
    "company": {
      "companyName": "Apple",
      "address": {
        "streetName": "Apple Park Way",
        "streetNumber": "1"
      }
    }
  },
  "hobby": {
    "hobbyName": "Music production",
    "genre": {
      "genreName": "Deep house",
      "genreYearOrigin": "1980"
    }
  }
}

See the following code example:

#create dict
jsonData = '{"name":"Chiel","industry":{"IndustryName":"Computer Science","company":{"companyName":"Apple","address":{"streetName":"Apple Park Way","streetNumber":"1"}}},"hobby":{"hobbyName":"Music production","genre":{"genreName":"Deep house","genreYearOrigin":"1980"}}}'
dictionary = json.loads(jsonData)

#Referencing dict for 'streetName', from array, hardcoded.    
companyElements = ["industry", "company", "address", "streetName"]
print(dictionary[companyElements[0]][companyElements[1]][companyElements[2]][companyElements[3]])

#Referencing dict for 'genreName', from array, hardcoded.    
hobbyElements = ["hobby", "genre", "genreName"]
print(dictionary[hobbyElements[0]][hobbyElements[1]][hobbyElements[2]])

The problem is that accessing the dictionaries is being done hardcoded. In other words, there are numbers being used (0, 1, 2, 3).

Is it possible to access the dictionairy through an array, but soft coded? So passing in an array (or another data structure) to the dict without making use of numbers? If so, how can one achieve this?

Chiel
  • 662
  • 1
  • 7
  • 30

3 Answers3

1

A possible solution is (from the example you provided):

def get_element(dictionary, array):
   x = dictionary.copy()
   for i in array:
      x = x[i]
   return x

companyElements = ["industry", "company", "address", "streetName"]
hobbyElements = ["hobby", "genre", "genreName"]

print(get_element(dictionary, companyElements))
print(get_element(dictionary, hobbyElements))
FMarazzi
  • 583
  • 1
  • 5
  • 14
1

You could write a function that iterates the given keys.

Beware that the following implementation will not catch exceptions if one or more keys are missing in your JSON:

import json
import copy
#create dict
jsonData = '{"name":"Chiel","industry":{"IndustryName":"Computer Science","company":{"companyName":"Apple","address":{"streetName":"Apple Park Way","streetNumber":"1"}}},"hobby":{"hobbyName":"Music production","genre":{"genreName":"Deep house","genreYearOrigin":"1980"}}}'
dictionary = json.loads(jsonData)

#Referencing dict for 'streetName', from array, hardcoded.
companyElements = ["industry", "company", "address", "streetName"]

#Referencing dict for 'genreName', from array, hardcoded.
hobbyElements = ["hobby", "genre", "genreName"]

def get_dict_value(data, keys):
    result = copy.deepcopy(data)
    for key in keys:
        result = result[key]
    return result

print( get_dict_value(dictionary, companyElements) )
print( get_dict_value(dictionary, hobbyElements) )

Result:

Apple Park Way
Deep house
Mike Scotty
  • 10,530
  • 5
  • 38
  • 50
1

You can use pandas library . It handles file operations very efficiently in Python because it's written in C. You could use json_normalize function in Pandas for this task .

Reference - https://www.kaggle.com/jboysen/quick-tutorial-flatten-nested-json-in-pandas

import json
file=open('kk.json')
inp=json.load(file)
print(json_normalize(inp))
kishan keswani
  • 85
  • 2
  • 10
  • Where is your code? Please use the example provided here. – Chiel Nov 23 '18 at 14:15
  • The example you have provided can be solved using normal dictionary way but if you have highly nested file , use json_normalize : import json file=open('kk.json') inp=json.load(file) print(json_normalize(inp)) – kishan keswani Nov 23 '18 at 17:35
  • I don't think this provides a solution that is asked for. Where is companyElements (the array of values used in the dot notation)? Where does it return the values "Apple Park Way" and "Deep House"? – Chiel Nov 24 '18 at 15:54
  • The documentation you provided is pretty useful though. I started experimenting with Pandas dataframe and it's performance. – Chiel Nov 24 '18 at 15:55
  • 1
    Hi Chiel , Deep house is there in hobby.genre.genreName and Apple Park Way is under industry.company.address.streetName . – kishan keswani Nov 24 '18 at 19:22
  • For data processing as well as file handling ,Pandas is the best library in Python . Also , I think while processing , your json may become highly nested , and Pandas would help you in that . – kishan keswani Nov 24 '18 at 19:25
  • Yes, I read that it's performance is the best for handeling huge amount of records: over 500k, which is exactly what I need. I have rewritten my ETL process using Pandas. – Chiel Nov 29 '18 at 08:56