3

I have a sample Json which contains key as fileName and value as filepath. For example:

{
  "sqlFiles":{
    "sqlQueryPath": "tmp/r/test.sql"
  },
  "csvFiles": {
    "firstSampleInput": "tmp/c/sample.csv",
    "secondSampleInput": "tmp/c/sample1.csv"
  }
}

and I have a function which takes key as input and return value as output. Something like this:

def readFilePath(key):
    with open('filePaths.json', 'r') as f:
        config = json.load(f)
        value = config[key]
        return value

If the key is available as a root element then my functions totally works but if the key is available in nested format just like it is available in the json then my function will fail.

I will call the function with the json path something like this:

readFilePath("sqlFiles.sqlQueryPath")

What changes to be made in the function that it parse the path in the format config["sqlFiles"]["sqlQueryPath"]

talkdatatome
  • 614
  • 1
  • 10
  • 17
  • You have iterate thru JSON keys and inner keys:values to get the values of any given key. – Chandu Sep 24 '18 at 11:11
  • Possible duplicate of [How to use a dot "." to access members of dictionary?](https://stackoverflow.com/questions/2352181/how-to-use-a-dot-to-access-members-of-dictionary) – Mike Scotty Sep 24 '18 at 11:19

5 Answers5

5

This is one approach. Using a simple iteration.

Demo:

key = "sqlFiles.sqlQueryPath"

def readFilePath(key):
    config = {
          "sqlFiles":{
            "sqlQueryPath": "tmp/r/test.sql"
          },
          "csvFiles": {
            "firstSampleInput": "tmp/c/sample.csv",
            "secondSampleInput": "tmp/c/sample1.csv"
          }
        }

    for i in key.split("."):
        if i in config:
            config = config[i]
        else:
            return None


    return config

print(readFilePath(key))
Rakesh
  • 81,458
  • 17
  • 76
  • 113
1

You need to split the key by '.' and read value iteratively, pesudo code:

for nestedKey in key.split('.'): value = value[nestedKey]

tomer.z
  • 1,033
  • 1
  • 10
  • 25
1

You could try this out,

def readFilePath(key):
with open('filePaths.json', 'r') as f:
    config = json.load(f)
    value = ""
    config_temp = config
    try:
        for k in key.split("."):
            config_temp = config_temp[k]
        value = config_temp
        return value
    except KeyError:
        return value
Narendra Prasath
  • 1,501
  • 1
  • 10
  • 20
0

You can try this by splitting the input and avoiding for loop with root key

def readFilePath(key):
    json_keys = key.split('.')
    with open('filePaths.json', 'r') as f:
        config = json.load(f)
        if len(json_keys) > 1:
            value = config[json_keys[0]][json_keys[1]]
        else:
            value = config[json_keys[0]]
        return value
Ashish
  • 623
  • 4
  • 10
0

Here is a solution:

def readFilePath(key):
    with open("sample.json") as f:
        config = json.load(f)
        value = None
        for k in key.split("."):
            try:
                value = config[k]
            except KeyError:
                config = value
        return value[k]
rockikz
  • 586
  • 1
  • 6
  • 17