0

I am writing a function that takes a string, splits it, and the uses json[key][key2][key3] formatting. The problem is n is potentially infinite (not literally but needs to written that way)

function getJsonValue(json,string) {
    var vals = string.split(".");
    var x = vals.length;
    var string = '';
    while (x != 0) {
      string += "['"+vals[(vals.length-x)]+"']"
        x--
    }
    return string;
}

That will produce, for example: "['condition']['item']['condition']['temp']"

I need to extract a value from that by attaching it to a json object, like

json"['condition']['item']['condition']['temp']"

But I don't know how or if that is even possible.

Edit: The problem is I need any value from a config file to be passed in and then parsed from a returning function. I.e. User knows the value will be condition.item.condition.temp for this specific query. I am trying to write one function that covers everything and pass in config values for what I know to be the output. So, on one query, I might want the condition.item.condition.temp value and on another I might want condition.wind.chill .

4
  • Are you outputting this value to another JavaScript file? If not, I don't believe this current implementation will work. ETA: One possible solution for this is if you generate the string and wish to inject it into a 'script' element in your project using jQuery but that's not elegant. Commented Jan 7, 2016 at 22:05
  • So what is the problem exactly? Is this code not working? Isn't the output what you want? You can also use this notation : json.key1.key2.key3 Commented Jan 7, 2016 at 22:07
  • Can you please sample a JSON and string? Commented Jan 7, 2016 at 22:07
  • No, just trying to attach it to an object. I am toying with some other ways to accomplish the task but similar manner, just not using strings. Commented Jan 7, 2016 at 22:08

3 Answers 3

2

I'm not sure if I understand 100% what you're trying to do, but if you're receiving a JS object json and a string in the format field1.field2.field3 and trying to get the value of json.field1.field2.field3 then you can do something like this:

function getJsonValue(json,string) {
    var vals = string.split(".");
    for (var i = 0; i < vals.length; i++) json = json[vals[i]];
    return json;
}

It would work like this for a given object:

var obj = { field1: { field2: { field3: "Hello!" } } };
var res = getJsonValue(obj, "field1.field2.field3"); 
console.log(res); // prints Hello
Sign up to request clarification or add additional context in comments.

3 Comments

I tried that earlier, and it kept hanging. No clue why.
It's not doing any input validation, if you want to share an input sample I can test here. It should work for the example I've sent, but if you give a set of fields that don't exist in the object, it will throw an error although you can easily fix that.
Yeah, I am not really sure why it isn't working right now. But I am going to mark this the answer because it has to be.
0

See lodash get

_.get(json, 'key1.key2.key3')

you can build the "path" from your current code and ask lodahs to get the value out for you.

Comments

0

What about doing an eval?

var json = {
    'one': {
        'two': {
            'three': {
                'four': 4
            }
        }
    }
};

alert(eval("json['one']['two']['three']['four']"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.