I would like to add to the an existing json a new value in the following path:
VAL:"BS"
, PATH:["info", "bosses", "lives"]
if my json had the passes it will add the value, otherwise I will create the fields
example:
var myJson = {
"root": {
"name": "jim",
"age": "4",
"info": {"bosses": {"name": "sam"}}
}
}
so my new json will look like this:
var myNewJson = {
"root": {
"name": "jim",
"age": "4",
"info": {"bosses": {"name": "sam", "lives": "BS"}}
}
}
an example when I didn't have the fields:
var myJson = {
"root": {
"name": "jim",
"age": "4",
}
}
the output:
var myNewJson = {
"root": {
"name": "jim",
"age": "4",
"info": {"bosses": {"lives": "BS"}}
}
}
an example where part of the path exists I didn't have the fields:
var myJson = {
"root": {
"name": "jim",
"age": "4",
"info": {"salary": 500}
}
}
the output:
var myNewJson = {
"root": {
"name": "jim",
"age": "4",
"info": {"salary": 500, "bosses": {"lives": "BS"}}
}
}
how can I check if path exists (note: part of the path might exist- how can I know from where to start it?)