I have a json object, and i need to add key value pair in the object at the given path.
const data = {
"hosts": {
"static": {
"domain": "http://api-azure-dev.atc.com",
"port": 80
},
"azure": {
"domain": "http://api-azure-dev.atc-v8.com",
"port": 80
}
},
"paths": {
"static": {
"cities": {
"hostKey": "static",
"path": "/cf/v2/lookups/cities",
"method": "GET"
}
}
}
};
here i need to add some value at path $.paths.static.getCountryCode
, this is a jsonpath notation, where $
represents root. then the updated value is returned.
const newConsul = {
"hostKey": "azure",
"path": "/v8/v1/lookups/countryCode/{country}",
"method": "get"
};
how to create a function which could insert the value of given newConsul into the data object? so that the final data object would look like:
const data = {
"hosts": {
"static": {
"domain": "http://api-azure-dev.atc.com",
"port": 80
},
"azure": {
"domain": "http://api-azure-dev.atc-v8.com",
"port": 80
}
},
"paths": {
"static": {
"cities": {
"hostKey": "static",
"path": "/cf/v2/lookups/cities",
"method": "GET"
},
"getCountryCode": {
"hostKey": "azure",
"path": "/v8/v1/lookups/countryCode/{country}",
"method": "get"
}
}
}
};