I'm currently working on a function that takes a string, splits it, and then formats it using json[key][key2][key3]
. The issue is that 'n' could potentially be infinite (not literally but needs to be written as such)
function getJsonValue(json,string) {
var vals = string.split(".");
var x = vals.length;
var newString = '';
while (x != 0) {
newString += "['"+vals[(vals.length-x)]+"']"
x--
}
return newString;
}
For example, this will produce:
"['condition']['item']['condition']['temp']"
I need to retrieve a value from that by combining it with a json object, like
json"['condition']['item']['condition']['temp']"
However, I am unsure if this is possible or how to achieve it.
Edit: The challenge I am facing is that I need any value from a config file to be passed in and then parsed by a returning function. For instance, a user knows the value will be condition.item.condition.temp for a specific query. I am attempting to create a single function that can handle all scenarios and pass in configuration values for the expected output. Therefore, in one query, the desired value might be condition.item.condition.temp and in another, it might be condition.wind.chill.