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