I have an API that response JSON data like this-
{
"unitcode":"apple",
"description":"bus",
"color":"red",
"intent":"Name 1"
}
I want to change like this-
{
"Value1":"apple",
"Value2":"bus",
"value3":"red",
"value4":"sale"
}
Presently, I can code to rename single key but i want some code to replace all key in one shot. my code like this-
request(baseurl)
.then( body => {
var unit = JSON.parse(body);
unit.intent = "sales"
unit.value1 = unit.unitcode
delete unit.unitcode;
console.log(unit)
console.log(unit.Value1)
var unit2 = JSON.stringify(unit)
// let code = unit.description;
conv.ask('Sales is 1 million metric tonnes ' + unit2);
})
please help me out on this and please little elaborate also to learn something new. thanks
Create a Map of original key to new key ( transformMap
). Convert the object to pairs of [key, value] with Object.entries()
, iterate with Array.map()
and replace the replacement key from the Map (or the original if not found). Convert back to an object with Object.toEntries()
:
const transformMap = new Map([ ['unitcode', 'Value1'], ['description', 'Value2'], ['color', 'Value3'], ['intent', 'Value4'] ]); const transformKeys = obj => Object.fromEntries( Object.entries(obj) .map(([k, v]) => [transformMap.get(k) || k, v]) ); const obj = { "unitcode": "apple", "description": "bus", "color": "red", "intent": "Name 1" }; const result = transformKeys(obj) console.log(result)
If you know the object structure and it is constant, you could just use destructing like so.
const data = { "unitcode":"apple", "description":"bus", "color":"red", "intent":"Name 1" }; // If the object is fixed and the fields are known. const mapData = ({ unitcode, description, color, intent }) => ({ Value1: unitcode, Value2: description, Value3: color, Value4: intent }); console.log(JSON.stringify(mapData(data)));
But if the object has an unknown number of properties:
const data = { "unitcode":"apple", "description":"bus", "color":"red", "intent":"Name 1" }; // If the object is fixed and the fields are known. const mapData = (data) => { return Object.keys(data).reduce((a,v,i) => { a[`Value${i+1}`] = data[v]; return a; }, {}); }; console.log(JSON.stringify(mapData(data)));
Well your target is to modify the keys and retain the value In that context, you can iterate through your data. To dynamically generate keys as Value1
, Value2
, etc, we will append Value
with iteration index which is going to be unique always.
const modifyInput = (input) => {
const modifiedInput = {}
Object.values(input).forEach((item, index) => {
modifiedInput[`Value${index + 1}`] = item
})
return modifiedInput
}
Use this function, pass your input and get your desired result
You can edit the array to have the values you need
let i=0,j=0,unit1={};
let unit = JSON.parse(body);
let unit3=["val1","val2","val3","val4"]
let unit5=Object.values(unit);
for(let key in unit){
unit1[unit3[i++]]=unit5[j++];
}
var unit2 = JSON.stringify(unit1)
console.log('Sales is 1 million metric tonnes \n' + unit2);
//Sales is 1 million metric tonnes
//{"val1":"apple","val2":"bus","val3":"red","val4":"Name 1"}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.