i have a requirement where the API response key will be changing depending upon the client for example if I have two client, client 1 and client 2 and my original json data is like below
{
"destination_addresses": [
"Washington, DC, USA",
"Philadelphia, PA, USA",
"Santa Barbara, CA, USA",
"Miami, FL, USA",
"Austin, TX, USA",
"Napa County, CA, USA"
],
"origin_addresses": [
"New York, NY, USA"
],
"rows": [{
"elements": [{
"distance": {
"text": "227 mi",
"value": 365468
},
"duration": {
"text": "3 hours 54 mins",
"value": 14064
},
"status": "OK"
},
{
"distance": {
"text": "94.6 mi",
"value": 152193
},
"duration": {
"text": "1 hour 44 mins",
"value": 6227
},
"status": "OK"
},
{
"distance": {
"text": "2,878 mi",
"value": 4632197
},
"duration": {
"text": "1 day 18 hours",
"value": 151772
},
"status": "OK"
},
{
"distance": {
"text": "1,286 mi",
"value": 2069031
},
"duration": {
"text": "18 hours 43 mins",
"value": 67405
},
"status": "OK"
},
{
"distance": {
"text": "1,742 mi",
"value": 2802972
},
"duration": {
"text": "1 day 2 hours",
"value": 93070
},
"status": "OK"
},
{
"distance": {
"text": "2,871 mi",
"value": 4620514
},
"duration": {
"text": "1 day 18 hours",
"value": 152913
},
"status": "OK"
}
]
}],
"status": "OK"
}
But for client 1 the key should be different like destination_addresses will be dest_address and for client 2 the key will be destaddress and this key change can be done till the child level like the distance key will be dist for client 1 and for client 2 it will be something else so for client 1
For ex the above response for client 1 would be like below
{
"dest_address": [
"Washington, DC, USA",
"Philadelphia, PA, USA",
"Santa Barbara, CA, USA",
"Miami, FL, USA",
"Austin, TX, USA",
"Napa County, CA, USA"
],
"og_addresses": [
"New York, NY, USA"
],
"row1": [{
"elements_client": [{
"dist": {
"t": "227 mi",
"v": 365468
},
"duration": {
"t": "3 hours 54 mins",
"value": 14064
},
"status": "OK"
},
{
"dist": {
"t": "94.6 mi",
"value": 152193
},
"duration": {
"t": "1 hour 44 mins",
"value": 6227
},
"status": "OK"
},
{
"dist": {
"t": "2,878 mi",
"value": 4632197
},
"duration": {
"t": "1 day 18 hours",
"value": 151772
},
"status": "OK"
},
{
"dist": {
"t": "1,286 mi",
"value": 2069031
},
"duration": {
"t": "18 hours 43 mins",
"value": 67405
},
"status": "OK"
},
{
"dist": {
"t": "1,742 mi",
"value": 2802972
},
"duration": {
"t": "1 day 2 hours",
"value": 93070
},
"status": "OK"
},
{
"dist": {
"t": "2,871 mi",
"value": 4620514
},
"duration": {
"t": "1 day 18 hours",
"value": 152913
},
"status": "OK"
}
]
}],
"status": "OK"
}
and it can be different keys for client 2, so how can I achieve this as the object can be nested and the client can be many, I tried a lot of thing like recursion and all but not able to do it and its client specific that's y at last posting my query here, below is the code that I am writing as u can see the parent key I can changes it but the nested i don't know how can I do that
const keys = {
"destination_addresses":"dest_address",
"origin_addresses" : "org_address"
}
const newObj = {};
recursivelyIterateProperties(obj)
function recursivelyIterateProperties(jsonObject,key) {
if(key){
if(keys[key]){
newObj[keys[key]] = jsonObject
}
}
console.log(jsonObject,'<----->>>>>',key)
if (jsonObject instanceof Array) {
const d = jsonObject[0];
if (typeof(d) === 'object'){
const obj = Object.keys(jsonObject)
for (var prop in obj) {
if (!(typeof(jsonObject[obj[prop]]) === 'string')) {
recursivelyIterateProperties(jsonObject[obj[prop]],obj[prop]);
}
}
}
// for (var i = 0; i < jsonObject.length; ++i) {
// recursivelyIterateProperties(jsonObject[i],key)
// }
}
else if (typeof(jsonObject) === 'object') {
const obj = Object.keys(jsonObject)
for (var prop in obj) {
if (!(typeof(jsonObject[obj[prop]]) === 'string')) {
recursivelyIterateProperties(jsonObject[obj[prop]],obj[prop]);
}else{
}
}
}
}
console.log(newObj);
See Question&Answers more detail:
os