Reputation: 786
This is my JSON input. I want to append data object before every object and convert into particular format given below. Also any plugins available for same will do.Convert JSON into particular format
[
{
"guid":"6FCBBB6B-4B5E-42C4-B108-12F594DABEE2",
"companyGuid":"12A5CD86-F6C6-455F-B27A-EFE587ED410D",
"parentLocationGuid":null,
"name":"location1",
"description":"test",
"isActive":true,
"row_num":2,
"children":[
{
"guid":"6FCBBB6B-4B5E-42C4-B108-12F594DABEE1",
"companyGuid":"12A5CD86-F6C6-455F-B27A-EFE587ED410D",
"parentLocationGuid":"6FCBBB6B-4B5E-42C4-B108-12F594DABEE2",
"name":"child location",
"description":"test child",
"isActive":true,
"row_num":1,
"children":[
]
}
]
}
]
I want output as :
[
{
"data":{
"guid":"6FCBBB6B-4B5E-42C4-B108-12F594DABEE2",
"companyGuid":"12A5CD86-F6C6-455F-B27A-EFE587ED410D",
"parentLocationGuid":null,
"name":"location1",
"description":"test",
"isActive":true,
"row_num":2
},
"children":[
{
"data":{
"guid":"6FCBBB6B-4B5E-42C4-B108-12F594DABEE1",
"companyGuid":"12A5CD86-F6C6-455F-B27A-EFE587ED410D",
"parentLocationGuid":"6FCBBB6B-4B5E-42C4-B108-12F594DABEE2",
"name":"child location",
"description":"test child",
"isActive":true,
"row_num":1
},
"children":[
]
}
]
}
]
Upvotes: 1
Views: 41
Reputation: 5224
Here is an ES6 implementation of what you're trying to make. This is adaptable to lists containing more than one element.
function formatter(input){
let output = new Array();
input.forEach( data_input=>{
let result = {};
let children = [];
result['data'] = {};
Object.keys(data_input).forEach(key => {
if (data_input[key] instanceof Object ){
if(!(data_input[key] instanceof Array && data_input[key].length===0 ))
children = data_input[key];
}
else{
result['data'][key] = data_input[key]
}
})
result['children'] = formatter(children);
output.push({'data': result['data']},{'children': result['children']})
})
return output
}
let myInput = [
{
"guid":"6FCBBB6B-4B5E-42C4-B108-12F594DABEE2",
"companyGuid":"12A5CD86-F6C6-455F-B27A-EFE587ED410D",
"parentLocationGuid":null,
"name":"location1",
"description":"test",
"isActive":true,
"row_num":2,
"children":[
{
"guid":"6FCBBB6B-4B5E-42C4-B108-12F594DABEE1",
"companyGuid":"12A5CD86-F6C6-455F-B27A-EFE587ED410D",
"parentLocationGuid":"6FCBBB6B-4B5E-42C4-B108-12F594DABEE2",
"name":"child location",
"description":"test child",
"isActive":true,
"row_num":1,
"children":[
]
}
]
}
]
console.log(formatter(myInput))
Upvotes: 1
Reputation: 171679
Fairly easy using ES6 deconstruction and spread operator
const res = data.map(({children,...otherProps})=>({data:{...otherProps}, children}));
console.log(res)
.as-console-wrapper {max-height: 100%!important;}
<script>
var data = [{
"guid": "6FCBBB6B-4B5E-42C4-B108-12F594DABEE2",
"companyGuid": "12A5CD86-F6C6-455F-B27A-EFE587ED410D",
"parentLocationGuid": null,
"name": "location1",
"description": "test",
"isActive": true,
"row_num": 2,
"children": [{
"guid": "6FCBBB6B-4B5E-42C4-B108-12F594DABEE1",
"companyGuid": "12A5CD86-F6C6-455F-B27A-EFE587ED410D",
"parentLocationGuid": "6FCBBB6B-4B5E-42C4-B108-12F594DABEE2",
"name": "child location",
"description": "test child",
"isActive": true,
"row_num": 1,
"children": [
]
}]
}]
</script>
Upvotes: 0