1

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

7
  • 2
    expected output is invalid Commented Oct 22, 2020 at 5:52
  • Also, this is not JSON, these are arrays with objects in them. Commented Oct 22, 2020 at 5:53
  • The desired result should be an object, not an array. Commented Oct 22, 2020 at 5:56
  • No you haven't. It still has [] around it when it should be {} Commented Oct 22, 2020 at 5:57
  • sorry. it is added now. Commented Oct 22, 2020 at 5:59

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

10 Comments

I have a situation where SERVER_COUNT has the same value and in such a situation it is not displaying all ErrorType values
const data1 = [ { "ErrorType": "Error-1A", "Error": "wrong ip address for 1A", "SERVER_COUNT": 9 }, { "ErrorType": "Error-1B", "Error": "password incorrect for 1B", "SERVER_COUNT": 9 }, ];
Object keys have to be unique. What are you expecting the result to be in that case?
{ "details1": { "9": { "ErrorType": "Error-1A", "Error": "wrong ip address for 1A" }, "9": { "ErrorType": "Error-1B", "Error": "password incorrect for 1B" } }, ...
It sounds like you need to revise your design to allow the value of the key to be an array of objects rather than a single object.
|
1

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

Comments

0

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

7 Comments

You have lots of typos
Where does this add the keys like details:, details2:, etc?
How would that do it? The key doesn't appear in cur anywhere.
if you know how works reduce, and how works Object assign then is obviously. you can reaplce this method in reduce by (acc, cur) => ({...acc, ...cur})... this just combine objects.
|
-1

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

Comments

Your Answer

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.