0

How can I convert this input

const input = [
    {"attributeGroup":"Compliance Information", "name":"specification", "value": "Medium"},
    {"attributeGroup":"Compliance Information", "name":"specification", "value": "High"},
    {"attributeGroup":"Compliance Information", "name":"complianceStatus", "value": "Pending"},
    {"attributeGroup":"Compliance Information", "name":"complianceStatus", "value": "Rejected"},
    {"attributeGroup":"Compliance Information", "name":"releasedDate", "value": "2023-08-30"},
    {"attributeGroup":"Compliance Information", "name":"releasedDate", "value":"2023-08-31"}
];

into this output

[{
    "ContextCode": "Compliance Information",
    "specification": "Medium",
    "complianceStatus": "Pending",
    "releasedDate": "2023-08-30"
},
{
    "ContextCode": "Compliance Information",
    "specification": "High",
    "complianceStatus": "Rejected",
    "releasedDate": "2023-08-31",
}
]

I tried solving that problem via ChatGPT but it does not return the expected output. Expected output is

[{
    "ContextCode": "Compliance Information",
    "specification": "Medium",
    "complianceStatus": "Pending",
    "releasedDate": "2023-08-30"
},
{
    "ContextCode": "Compliance Information",
    "specification": "High",
    "complianceStatus": "Rejected",
    "releasedDate": "2023-08-31",
}
]
Abra
  • 19,142
  • 7
  • 29
  • 41
  • 1
    That looks like JSON. Is it? Also, do you want to write code to do the conversion? If so, what language? – Abra Aug 31 '23 at 17:57

1 Answers1

0

There could be a better approach to this:

const input = [{"attributeGroup":"Compliance Information", "name":"specification", "value": "Medium"},{"attributeGroup":"Compliance Information", "name":"specification", "value": "High"},{"attributeGroup":"Compliance Information", "name":"complianceStatus", "value": "Pending"},{"attributeGroup":"Compliance Information", "name":"complianceStatus", "value": "Rejected"},{"attributeGroup":"Compliance Information", "name":"releasedDate", "value": "2023-08-30"},{"attributeGroup":"Compliance Information", "name":"releasedDate", "value":"2023-08-31"}];
        
let response = [];

for(let i=0;i<(input.length/3);i++){
    let second = i+2,
        third = second+2;
    
    response.push({
        "contextCode": input[i].attributeGroup,
        "specification": input[i].value,
        "complianceStatus": input[second].value,
        "releasedDate": input[third].value,
    });
}

console.log(response);