JSON from Rest API map headers to replace key in rows

I have an API response which has the format below

I would like to display the information from "headerIdToValue" in "rows" in a table. I can extract this information using a transformer. However, the key is 24 character ID.

"headers" contains the decoder for the keys, is there a way to replace the 24 character ID with the "name" from "headers"?

Hey @pierrelin!

I don't have your exact data setup, so you'll likely need to tweak the Javascript I wrote, but my code should be doing something similar. I'm looping through data (an array of row objects) and changing each key in the row object to the key found in my cipher (called 'ref' on line 2).

Let me know how this works for you :slight_smile:

const sourceData = table2.data;
const ref = [{id:1, name:'id'},{id:2, name:'firstName'},{id:3, name:'role'}];

function replaceKeysWithNumbers(data) {
  return data.map(item => {
    const newItem = {};
    for (const key in item) {
      const mapping = ref.find(entry => entry.name === key);
      if (mapping) {
        newItem[mapping.id] = item[key];
      } else {
        newItem[key] = item[key];
      }
    }
    return newItem;
  });
}

const newData = replaceKeysWithNumbers(sourceData);

return newData;