'Javascript : How convert existing JSON Object into custom key based Object? [closed]

I need to convert/destructure my existing JSON object into a new form with key-based JSON, please look at the below input JSON

Note : Below array has n numbers of Objects.

INPUT JSON:

[
    {
        "processId": 95,
        "pid": "PID_0",
        "actionData": [
            {
                "actionId": 96,
                "action": "Sbend",
                "avg": 0.09134998917579651,
                "avgVid": "",
                "max": 0.13287270069122314,
                "maxVid": ",
                "maxPos": 1,
                "min": 0.06643635034561157,
                "minVid": "",
                "minPos": 0,
                "total": 8,
                "totalTime": null
            },
            {
                "actionId": 98,
                "action": "Stand",
                "avg": 0.7355453372001648,
                "avgVid": "",
                "max": 2.889981508255005,
                "maxVid": "",
                "maxPos": 11,
                "min": 0.06643635034561157,
                "minVid": "",
                "minPos": 9,
                "total": 28,
                "totalTime": null
            }
        ]
    }
]

Here I need is take the 'pid' key value and set it to the root key for existing JSON, Below there is an expected output

Expected OUTPUT

[
    "PID_0" : {
        "processId": 95,
        "actionData": [
            {
                "actionId": 96,
                "action": "Sbend",
                "avg": 0.09134998917579651,
                "avgVid": "",
                "max": 0.13287270069122314,
                "maxVid": ",
                "maxPos": 1,
                "min": 0.06643635034561157,
                "minVid": "",
                "minPos": 0,
                "total": 8,
                "totalTime": null
            },
            {
                "actionId": 98,
                "action": "Stand",
                "avg": 0.7355453372001648,
                "avgVid": "",
                "max": 2.889981508255005,
                "maxVid": "",
                "maxPos": 11,
                "min": 0.06643635034561157,
                "minVid": "",
                "minPos": 9,
                "total": 28,
                "totalTime": null
            }
        ]
    }
]

Code I'm using to achieve this, but its not giving correct result to me,

let b = Object.keys(itemsValues).reduce(
  (p, c) => { console.log(p)
    for (let item of itemsValues[c].actionData) {
      p.pid.actionData.push(item);
    }
    return p;
  }, {
    pid: {
      actionData: [],
    },
  }
);

Kindly help me out to fix this issue, its really helps me a lot. Thanks.



Solution 1:[1]

  1. reduce the array into the object you are looking for as output
  2. set new key for object based on the received pid
  3. then remove that pid from its original position

In terms of code:

const result = input.reduce((finalObject, obj) => {
    finalObject[obj.pid] = obj;
    delete obj.pid;
    return finalObject;
}, {});

Following is working snippet:

const input = [{
  processId: 95,
  pid: 'PID_0',
  actionData: [{
      actionId: 96,
      action: 'Sbend',
      avg: 0.09134998917579651,
      avgVid: '',
      max: 0.13287270069122314,
      maxVid: '',
      maxPos: 1,
      min: 0.06643635034561157,
      minVid: '',
      minPos: 0,
      total: 8,
      totalTime: null,
    },
    {
      actionId: 98,
      action: 'Stand',
      avg: 0.7355453372001648,
      avgVid: '',
      max: 2.889981508255005,
      maxVid: '',
      maxPos: 11,
      min: 0.06643635034561157,
      minVid: '',
      minPos: 9,
      total: 28,
      totalTime: null,
    },
  ],
}, ];

const result = input.reduce((finalObject, obj) => {
  finalObject[obj.pid] = obj;
  delete obj.pid;
  return finalObject;
}, {});

console.log(result)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aneesh