Reputation: 71
This is the structure that i am having, i converted from the xml to json, but now i dont need the root element and also the element tag:
{
"root": {
"batchRequests": [
{
"element": {
"method": "PATCH",
"url": "url",
"richInput": {
"Error_Message__c": {},
"Status__c": "0"
}
}
},
{
"element": {
"method": "PATCH",
"url": "url",
"richInput": {
"Error_Message__c": {},
"Status__c": "0"
}
}
}
]
}
And I want to transform that into this structure:
{
"batchRequests": [
{
"method": "PATCH",
"url": "url",
"richInput": {
"Error_Message__c": "",
"Status__c": "0"
}
},
{
"method": "PATCH",
"url": "url",
"richInput": {
"Error_Message__c": "",
"Status__c": "1"
}
}
]
}
Can you please help me with this transformation, if it is possible from javascript?
Upvotes: 1
Views: 58
Reputation: 1839
You can do it with this single line.
yourObject = { batchRequests: yourObject.root.batchRequests.map((x) => x.element) }
Try it here.
var yourObject = {
"root": {
"batchRequests": [{
"element": {
"method": "PATCH",
"url": "url",
"richInput": {
"Error_Message__c": {},
"Status__c": "0"
}
}
},
{
"element": {
"method": "PATCH",
"url": "url",
"richInput": {
"Error_Message__c": {},
"Status__c": "0"
}
}
}
]
}
}
yourObject = { batchRequests: yourObject.root.batchRequests.map((x) => x.element) }
console.log(yourObject);
Upvotes: 2
Reputation: 1504
Parse batchRequests
with map
function:
const batchRequests = data1.root.batchRequests.map(el => el.element)
Create new object with this data:
const data2 = { batchRequests }
const data1 = {
"root": {
"batchRequests": [
{
"element": {
"method": "PATCH",
"url": "url",
"richInput": {
"Error_Message__c": {},
"Status__c": "0"
}
}
},
{
"element": {
"method": "PATCH",
"url": "url",
"richInput": {
"Error_Message__c": {},
"Status__c": "0"
}
}
}
]
}
}
const batchRequests = data1.root.batchRequests.map(el => el.element)
const data2 = { batchRequests }
console.log(data2)
Upvotes: 0
Reputation: 4542
If your json item is called json
:
let result = {batchRequests:[]};
for(let item of json.root.batchRequests){
result.batchRequests.push(item.element);
}
Upvotes: 0