4
\$\begingroup\$

Issue

Our API returns two formats JSON and this SQL table format. I'm not sure what it is truly called so I'll give a few examples below. Like all API responses, it can be a list or a single response but it is always in the same format. I'm creating a utility function for this SQL table format to be converted into a JSON object so the front-end only needs to be concerned with a single format, JSON.

Problem

Convert SQL Table Response to JSON

Examples of the SQL table format

Here is the SQL table format as a success message.

{
 "COLUMNS":["SUCCESS","RETMESSAGE"],
 "DATA":[[true,"Order Approved"]]
}

Here is an example of a list

{
 "COLUMNS":["PRODUCTID","PRODUCTNAME"],
 "DATA":[[1001,"Product1"],[1002,"Product2"],[1003,"Product3"]]
}

Current Solution

The current solution is the following. It works well but I keep coming back to it and thinking there is a more elegant way of writing this.

const request = {
 "COLUMNS":["SUCCESS","RETMESSAGE"],
 "DATA":[[true,"Order Approved"]]
 };
const desiredFormat = [{
 "SUCCESS":true,
 "RETMESSAGE":"Order Approved"
 }];
function tableToJSON (dataset) {
 const data = dataset.DATA;
 const columns = dataset.COLUMNS;
 const jsonData = [];
 data.forEach( (row) => {
 const json = {};
 row.forEach( (item, index) => {
 json[columns[index]] = item;
 });
 jsonData.push(json);
 });
 return jsonData;
 }
const formattedResponse = tableToJSON(request);
console.log(JSON.stringify(formattedResponse) === JSON.stringify(desiredFormat))
// Outputs: True
Sᴀᴍ Onᴇᴌᴀ
29.5k16 gold badges45 silver badges201 bronze badges
asked Jul 16, 2019 at 19:05
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Is that better? \$\endgroup\$ Commented Jul 16, 2019 at 19:32
  • \$\begingroup\$ Ah you want to pivot data. Now it's clear to me. \$\endgroup\$ Commented Jul 16, 2019 at 19:34

1 Answer 1

3
\$\begingroup\$

Whenever you have code that sets up an array, then has a loop to push items into that array, Array.map() could be used to condense that code. For example, these lines:

const jsonData = [];
data.forEach( (row) => {
 const json = {};
 row.forEach( (item, index) => {
 json[columns[index]] = item;
 });
 jsonData.push(json);
});
return jsonData;

Could be simplified to this:

return data.map( (row) => {
 const json = {};
 row.forEach( (item, index) => {
 json[columns[index]] = item;
 });
 return json;
});

You could optionally simplify the inner loop using Array.reduce()

return data.map( (row) => {
 return row.reduce( (json, item, index) => {
 json[columns[index]] = item;
 return json;
 }, {});
});

There are some good exercises in Functional Programming in Javascript where you practice implementing some of those methods.

answered Jul 16, 2019 at 20:57
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.