-2

I have a Java script object and I am trying to dynamically access a specific value from it and change it to something

My object:

myJson =  {
            "id" : "http://**********",
            "$schema": "http://json-schema.org/draft-04/schema",
            "type" : "object",
            "properties":{
            "AddressLine1" :{"type":"string" , "maxLength":62},
            "AddressLine2" :{"type":"string" , "maxLength":62},
            "city" :{"type":"string" , "maxLength":62},
            "state" :{"type":"string" , "maxLength":5}
        },
        "required": ["AddressLine1", "city"]
        }

for example if I need to access maxLength in state object dynamically based on the given "path"

path = "$.properties.state.maxLength";

So based on the variable "path" that I get I have to access the value based on the path and edit it.

Full code:

var sample = function (){


    var myJson = {
        "id" : "http://application.nw.com/address",
        "$schema": "http://json-schema.org/draft-04/schema",
        "type" : "object",
        "properties":{
        "AddressLine1" :{"type":"string" , "maxLength":62},
        "AddressLine2" :{"type":"string" , "maxLength":62},
        "city" :{"type":"string" , "maxLength":62},
        "state" :{"type":"string" , "maxLength":5}
    },
    "required": ["AddressLine1", "city"]
    }

    // I get the path from an external source.
    path = "$.properties.state.maxLength";

    path = path.substring('$.'.length);
    console.log(path);          // log = properties.state.maxLength
    console.log(myJson.properties.state.maxLength);        // log 5
    console.log(myJson.path);                      // log undefined


}

I am a newbie pls help and try to encourage, if I did something really silly.

Thanks

please feel free to edit it if any mistakes

kasey
  • 185
  • 1
  • 12
  • 6
    That's not JSON, that's a JavaScript object. – Biffen May 04 '16 at 17:52
  • 3
    have you tried `path = myJson.properties.state.maxLength;`? – Felippe Duarte May 04 '16 at 17:53
  • @FelippeDuarte has the right answer. You need to point to the appropriate object first as `properties` does not exist on its own – colecmc May 04 '16 at 17:55
  • Felippe Duarte I have tried path = myJson.properties.state.maxLength; and i get the answer, but i get a path , where path = $.properties.state.maxLength and then I operate on it and set path = properties.state.maxLength. – kasey May 04 '16 at 17:59
  • 1
    Please, show us your entire code. – Felippe Duarte May 04 '16 at 18:01
  • 1
    If you need to dynamically access the properties of a javascript object then you need to use the bracket notation. For example `myJson["properties"]` vs `myJson.properties` – Matt Burland May 04 '16 at 18:02
  • related: http://stackoverflow.com/questions/4968406/javascript-property-access-dot-notation-vs-brackets – Matt Burland May 04 '16 at 18:06
  • @MattBurland it is similar to the one you suggested but this has more layers and i think this is a deep access. – kasey May 04 '16 at 19:17
  • @FelippeDuarte I have updated he question. – kasey May 04 '16 at 19:40
  • @FelippeDuarte , I have tried that, gives me undefined. – kasey May 04 '16 at 20:22
  • What are you trying to do with `path`? It does not exist on `myJson`, so logging it will be undefined. – trenthaynes May 04 '16 at 22:15
  • @whipdancer I have changed the question so it adds more meaning. I am trying to dynamically access values from the javascript object. For example let us say I have to find the value of maxLength in state object in properties, how do i do it. The value to be accessed keeps changing for every instance and I should be able to edit the maxLength as well. – kasey May 05 '16 at 13:28

1 Answers1

1

You need to parse the path in order to access the properties.

Because path is a variable, myJson.path doesn't make sense. The dot notation indicates a property.

Because you need to dynamically access a property based on the content of the variable, you need to use bracket notation. Each level of depth you go down in the object needs its own bracket.

console.log(myJson) --> the object
console.log(myJson["properties"]  --> the properties property of the object
console.log(myJson["properties"]["state"] --> the state property of the properties property

Using the bracket notation, you can parse the path variable into its component parts:

var p1 = "properties";
var p2 = "states";
var p3 = "maxLength";
console.log(myJson[p1][p2][p3]);

Here is a fiddle demo.

trenthaynes
  • 1,668
  • 2
  • 16
  • 28