I have this JSON Object:
{
"columnNames": [
"Incident ID",
"IncidentType"
],
"rows": [
[
"3599590",
"Telecommuting/VWA Empl- Initiate"
],
[
"3599601",
"Telecommuting/VWA Empl- Initiate"
]
]
}
I would like to convert that object in Javascript to this object:
{
reportResults: [{
"Incident ID": "3599590",
"IncidentType": "Telecommuting/VWA Empl- Initiate"
},
{
"Incident ID": "3599591",
"IncidentType": "Telecommuting/VWA Empl- Initiate"
}
]
}
I have tried using the push function in the following example:
VWA_Output = {
"columnNames": [
"Incident ID",
"IncidentType"
],
"rows": [
[
"3599590",
"Telecommuting/VWA Empl- Initiate"
],
[
"3599601",
"Telecommuting/VWA Empl- Initiate"
]
]
};
JSTest_JSON_Var1 = {
reportResults: []
};
for (i in VWA_Output.rows) {
for (var j in VWA_Output.rows[i]) {
var key = VWA_Output.columnNames[j];
var value = VWA_Output.rows[i][j]
JSTest_JSON_Var1.reportResults.push({
[key]: value
});
}
}
console.log(JSTest_JSON_Var1);
However, it seems to create the the object like this with the collection as an individual array element:
{
[{
"reportResults": [{
"Incident ID": "3599590"
}, {
"IncidentType": "Telecommuting/VWA Empl- Initiate"
}
},
{
"Incident ID": "3599591"
},
{
"IncidentType": "Telecommuting/VWA Empl- Initiate"
}
}]
}
I would like the collection of columns and rows to be a single record collection in the array:
{
"reportResults": [{
"Incident ID": "3599590",
"IncidentType": "Telecommuting/VWA Empl- Initiate"
}, {
"Incident ID": "3599591",
"IncidentType": "Telecommuting/VWA Empl- Initiate"
}]
}
Thanks!
4 Answers 4
Suppose your example data is in data:
const result = {
"reportResults" : data.rows.map(row => {
return {
[data.columnNames[0]]: row[0],
[data.columnNames[1]]: row[1]
}
})
}
Comments
Define the reportResults all at once - have its contents be an array mapped from the rows, where you use the index of the column name you're iterating over to access the appropriate row value. I'd use Object.fromEntries to keep things concise.
const input = {
"columnNames": [
"Incident ID",
"IncidentType"
],
"rows": [
[
"3599590",
"Telecommuting/VWA Empl- Initiate"
],
[
"3599601",
"Telecommuting/VWA Empl- Initiate"
]
]
};
const output = {
reportResults: input.rows.map(row => Object.fromEntries(
input.columnNames.map((name, i) => [name, row[i]])
))
};
console.log(output);
Comments
that is my solution:
var data = {
"columnNames": [
"Incident ID",
"IncidentType"
],
"rows": [
[
"3599590",
"Telecommuting/VWA Empl- Initiate"
],
[
"3599601",
"Telecommuting/VWA Empl- Initiate"
]
]
}
var reportResults = data.rows.map((row) =>
Object.assign({}, ...row.map((cell, i) => ({ [data.columnNames[i]]: cell})))
)
console.log({reportResults})
Not optimal, but short)
Comments
See my comments above. Every answer provided worked in the browser, but not in the SOA Suite Javascript component I am using. The component didn't like the map function calls. Thanks again for all of the responses.
Here is what did work with the Oracle SOA Suite JS component:
process.VWA_Output = {
"columnNames": [
"Incident ID",
"IncidentType"
],
"rows": [
[
"3599590",
"Telecommuting/VWA Empl- Initiate"
],
[
"3599601",
"Telecommuting/VWA Empl- Initiate"
]
]
};
process.JSTest_JSON_Var1 = {
reportResults: []
};
for (i in process.VWA_Output.rows) {
const row = new Object();
for (var j in process.VWA_Output.rows[i]) {
var key = process.VWA_Output.columnNames[j];
var value = process.VWA_Output.rows[i][j];
row[key] = value;
}
process.JSTest_JSON_Var1.reportResults.push(row);
}
Comments
Explore related questions
See similar questions with these tags.
obj[key] = valueand finally outside the innerloop push that object toJSTest_JSON_Var1.reportResults. But I think you should just go with the more readable answers provided below.