Matthew Martinez
Matthew Martinez

Reputation: 13

Create JSON object from Array and Multi-Dimensional Array

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!

Upvotes: 0

Views: 886

Answers (4)

Matthew Martinez
Matthew Martinez

Reputation: 13

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); 
}

Upvotes: 1

artem
artem

Reputation: 457

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)

Upvotes: 1

Dan Oswalt
Dan Oswalt

Reputation: 2189

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]
    }
  })
}

Upvotes: 2

CertainPerformance
CertainPerformance

Reputation: 370639

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);

Upvotes: 1

Related Questions