Using Code node to process two JSONs

I have two JSON output from previous node, simple format like this:

[
    {
        "id": "HDarLEZSZowtXqhblWXxfg",
        "name": "Profile"
    },
    {
        "id": "HK_nEu__ciKtKBo3R4IGTw",
        "name": "Orders"
    }
]
[
    {
        "id": "HAlbab7tFBNezyd5nFq3RA",
        "name": "Profile"
    },
    {
        "id": "HFazxo5KT5bAwm7R_jZNxQ",
        "name": "Orders"
    }
]

I want to output a mapping like this based on the name:

{
  "HDarLEZSZowtXqhblWXxfg": "HAlbab7tFBNezyd5nFq3RA",  // Profile
  "HK_nEu__ciKtKBo3R4IGTw": "HFazxo5KT5bAwm7R_jZNxQ"   // Orders
}

I asked ChatGPT and it gives me the code as follow:

// Load both lists
const oldList = $('Old List').items.map(item => item.json); // e.g. Profile, Orders
const newList = $('New List').items.map(item => item.json); // Full CDP schema

// Create a name -> id map from newList
const newMap = {};
for (const entry of newList) {
  newMap[entry.name] = entry.id;
}

// Build replacement map: old ID -> new ID (when name matches)
const replacements = {};
for (const entry of oldList) {
  const newId = newMap[entry.name];
  if (newId) {
    replacements[entry.id] = newId;
  }
}

return [
  {
    json: replacements
  }
];

However, it is not working. Error message “Cannot read properties of undefined (reading ‘map’) [line 2]”. The “.items” is red underlined, seems it is not a correct function.
May I know what is the correct way to do it?

Many Thanks,
Kai

Hey @Kai_C

Try this

const oldList = $('Old List').all().map(item => item.json);
const newList = $('New List').all().map(item => item.json);

// Create mapping
const replacements = {};
oldList.forEach(oldItem => {
    const newItem = newList.find(newItem => newItem.name === oldItem.name);
    if (newItem) {
        replacements[oldItem.id] = newItem.id;
    }
});

return [{ json: replacements }];
3 Likes

Thanks it works!