meallhour
meallhour

Reputation: 15581

How to modify JSON response array to add new keys in NodeJS?

I have a NodeJS function which is returning the following JSON array. It has all errors of type 1:

const data1 =
[
  {
    "ErrorType": "Error-1A",
    "Error": "wrong ip address for 1A",
    "SERVER_COUNT": 9
  },
  {
    "ErrorType": "Error-1B",
    "Error": "password incorrect for 1B",
    "SERVER_COUNT": 2
  },
....
]

I have another JSON array for all errors of type 2:

const data2 = 
[
  {
    "ErrorType": "Error-2A",
    "Error": "wrong data for 2A",
    "SERVER_COUNT": 8
  },
  {
    "ErrorType": "Error-2B",
    "Error": "password incorrect for 2B",
    "SERVER_COUNT": 3
  },
....
]

I have another JSON array for all errors of type 3:

const data3 = 
[
  {
    "ErrorType": "Error-3A",
    "Error": "wrong data for 3A",
    "SERVER_COUNT": 1
  },
  {
    "ErrorType": "Error-3B",
    "Error": "password incorrect for 3C",
    "SERVER_COUNT": 5
  },
....
]

I want to combine the 3 JSON arrays data1, data2, data3 and the final JSON object should look as below:

{
  "details1": {
   "9": {
    "ErrorType": "Error-1A",
    "Error": "wrong ip address for 1A"
   },
      "2": {
    "ErrorType": "Error-1B",
    "Error": "password incorrect for 1B"
   }
},
  "details2": {
   "8": {
    "ErrorType": "Error-2A",
    "Error": "wrong ip address for 2A"
   },
      "3": {
    "ErrorType": "Error-2B",
    "Error": "password incorrect for 2B"
   }
},
  "details3": {
   "1": {
    "ErrorType": "Error-3A",
    "Error": "wrong ip address for 3A"
   },
      "5": {
    "ErrorType": "Error-3B",
    "Error": "password incorrect for 3B"
   }
}
}

Please note that I want to add 3 new keys - details1, details2 and details3 - in the final response. I also want to take out the SERVER_COUNT value and make it as a key.

I have added the following code but not sure to update it so as to get the final desired json

let finalData = Object.assign({}, ...data1.map(({
  SERVER_COUNT,
  ...rest
}) => ({
  [SERVER_COUNT]: rest
})));

Note: There are only 3 main keys and those are hard-coded details1 , details2, details13

Upvotes: 1

Views: 670

Answers (4)

Nick Parsons
Nick Parsons

Reputation: 50684

You can put all your data arrays into one array called all_data, which you can then use .map() on to form an array of [detailsN, object] entries. Here detailsN represents your key for your outer object and the object represents the object for each detail key. Once you have mapped your all_data to entries, you can use Object.fromEntries() to convert it into an object.

The object component for the entires can be formed by mapping the data from each data array into [SERVER_COUNT, obj] entries, which you can then called Object.fromEntries() to convert the array of mapped entries into an object.

const data1 = [ { "ErrorType": "Error-1A", "Error": "wrong ip address for 1A", "SERVER_COUNT": 9 }, { "ErrorType": "Error-1B", "Error": "password incorrect for 1B", "SERVER_COUNT": 2 }, ];
const data2 = [ { "ErrorType": "Error-2A", "Error": "wrong data for 2A", "SERVER_COUNT": 8 }, { "ErrorType": "Error-2B", "Error": "password incorrect for 2B", "SERVER_COUNT": 3 }, ];
const data3 = [ { "ErrorType": "Error-3A", "Error": "wrong data for 3A", "SERVER_COUNT": 1 }, { "ErrorType": "Error-3B", "Error": "password incorrect for 3C", "SERVER_COUNT": 5 }, ];

const keys = ["details1", "details2", "details3"];
const all_data = [data1, data2, data3];
const res = Object.fromEntries(all_data.map((data, i) => [
  keys[i], 
  Object.fromEntries(data.map(({SERVER_COUNT, ...r}) => [SERVER_COUNT, r]))
]));
console.log(res);

Upvotes: 1

Robert
Robert

Reputation: 2753

JS pseudokode:.

  1. Parse all joson to objects
  2. Object.assigne or {...obj1, ...objt2};
  3. transform object to json
const data1 =
[
  {
    "ErrorType": "Error-1A",
    "Error": "wrong ip address for 1A",
    "SERVER_COUNT": 9
  },
  {
    "ErrorType": "Error-1B",
    "Error": "password incorrect for 1B",
    "SERVER_COUNT": 2
  }
]

const data2 = 
[
  {
    "ErrorType": "Error-2A",
    "Error": "wrong data for 2A",
    "SERVER_COUNT": 8
  },
  {
    "ErrorType": "Error-2B",
    "Error": "password incorrect for 2B",
    "SERVER_COUNT": 3
  }
]

const data3 = 
[
  {
    "ErrorType": "Error-3A",
    "Error": "wrong data for 3A",
    "SERVER_COUNT": 1
  },
  {
    "ErrorType": "Error-3B",
    "Error": "password incorrect for 3C",
    "SERVER_COUNT": 5
  }
]

console.log([data1, data2, data3].reduce((acc, cur, i) => Object.assign(acc, {["detailist" + i]: {...cur}}), {}))

JS BIN

Upvotes: 0

LT-Sites
LT-Sites

Reputation: 415

If you have an object structure like myObj={} you can add keys or properties like:

myObj.myKeyOrProperty = 'The data here';

// or

myObj.['myKeyOrProperty'] = 'The data here';

you need to serialize or deserialize to or from JSON to do that in request / response.

If you have a JSON response like an array of objects use:

array.push(myAwesomeObj-A);
array.push(myAwesomeObj-B);
array.push(myAwesomeObj-C);
etc...

When you have the desired data structure emit the response as:

HTTP/1.1 200 OK
Content-Type: application/vnd.api+json
{
"myDataKey": ---your data must be here in a JSON string format---
}

Upvotes: -1

Barmar
Barmar

Reputation: 780889

Loop over the array elements, turning each of them into an object property using the SERVER_COUNT property as the key.

function dataToDetails (data) {
  let result = {};
  data.forEach(({ErrorType, Error, SERVER_COUNT}) => result[SERVER_COUNT] = {ErrorType, Error});
  return result;
}

const data1 =
[
  {
    "ErrorType": "Error-1A",
    "Error": "wrong ip address for 1A",
    "SERVER_COUNT": 9
  },
  {
    "ErrorType": "Error-1B",
    "Error": "password incorrect for 1B",
    "SERVER_COUNT": 2
  },

];

const data2 = 
[
  {
    "ErrorType": "Error-2A",
    "Error": "wrong data for 2A",
    "SERVER_COUNT": 8
  },
  {
    "ErrorType": "Error-2B",
    "Error": "password incorrect for 2B",
    "SERVER_COUNT": 3
  },

];

const data3 = 
[
  {
    "ErrorType": "Error-3A",
    "Error": "wrong data for 3A",
    "SERVER_COUNT": 1
  },
  {
    "ErrorType": "Error-3B",
    "Error": "password incorrect for 3C",
    "SERVER_COUNT": 5
  },

];


let finalData = {
  details1: dataToDetails(data1),
  details2: dataToDetails(data2),
  details3: dataToDetails(data3)
};

console.log(finalData);

Upvotes: 1

Related Questions