How to update a JSON based on two another JSON using Code or any other note

Hi there im trying to do the Following using Code node or any other more clever way

This is a Mock Workflow

Mock Data JSON:

[
  {
    "parent": "color",
    "abbr": "BLA",
    "attribute_value": "Black"
  },
  {
    "parent": "color",
    "abbr": "WHI",
    "attribute_value": "White"
  },
  {
    "parent": "color",
    "abbr": "MAR",
    "attribute_value": "Maroon"
  },
  {
    "parent": "logo_materialor",
    "abbr": "ORA FL",
    "attribute_value": "Orange Fluo"
  },
  {
    "parent": "logo_material",
    "abbr": "CL",
    "attribute_value": "Classic "
  },
  {
    "parent": "logo_material",
    "abbr": "FL",
    "attribute_value": "Flokos"
  },
  {
    "parent": "logo_material",
    "abbr": "EM",
    "attribute_value": "Embroidery"
  },
  {
    "parent": "size",
    "abbr": "XS",
    "attribute_value": "Extra Small"
  },
  {
    "parent": "logo_material",
    "abbr": "BR",
    "attribute_value": "Classic Brushed"
  },
  {
    "parent": "color",
    "abbr": "CHA",
    "attribute_value": "Charcoal"
  }
]

NODE1 JSON :

[
  {
    "item_Name": "Beanie-BLA",
    "base_product": "Beanie",
    "logo": null,
    "logo_name": null,
    "logo_material": null,
    "logo_material_name": null,
    "color": "BLA",
    "color_name": null,
    "logo_color": null,
    "logo_color_name": null,
    "size": null,
    "size_name": null,
    "item_group": "Beanies",
    "variant_of": "Beanie"
  },
  {
    "item_Name": "Beanie-CHA",
    "base_product": "Beanie",
    "logo": null,
    "logo_name": null,
    "logo_material": null,
    "logo_material_name": null,
    "color": "CHA",
    "color_name": null,
    "logo_color": null,
    "logo_color_name": null,
    "size": null,
    "size_name": null,
    "item_group": "Beanies",
    "variant_of": "Beanie"
  }
]

Wanted Result :

[
  {
    "item_Name": "Beanie-BLA",
    "base_product": "Beanie",
    "logo": null,
    "logo_name": null,
    "logo_material": null,
    "logo_material_name": null,
    "color": "BLA",
    "color_name": "Black",
    "logo_color": null,
    "logo_color_name": null,
    "size": null,
    "size_name": null,
    "item_group": "Beanies",
    "variant_of": "Beanie"
  },
  {
    "item_Name": "Beanie-CHA",
    "base_product": "Beanie",
    "logo": null,
    "logo_name": null,
    "logo_material": null,
    "logo_material_name": null,
    "color": "CHA",
    "color_name": "Charcoal",
    "logo_color": null,
    "logo_color_name": null,
    "size": null,
    "size_name": null,
    "item_group": "Beanies",
    "variant_of": "Beanie"
  }
]

What im trying to do is :

If abbr from Mock Data = a value of the JSON property from NODE1
then
matchProperty_name = attribute_value from Mock Data

For example if

abbr = “BLA” then it matches “color”:“BLA” then i want “color_name”:attribute_value, means “color_name”:“Black”

Same for the other properties.

Maybe this can be done directly with other nodes but i havent found a way and with code node i failed caus eof my coding skills.

You can “enrich” your node1 with the mock “lookup” data and then replace the color with the attribute_value field along with any other fields.

Oh cool, I just realised there’s a rename keys node now… same thing with renaming instead of copying the value.

Thank you @pemontto that is ok for starters but what i want to do the same for every property ends with '_name"

lets make a parenthesis that will help me find my mistake, for this workflow :

I was expecting this result :

[
  {
    "item_Name": "Crewneck-ZF-CL-BLA-WHI-XL",
    "base_product": "Crewneck",
    "logo": "ZF",
    "logo_name": "H79b3feEa4",
    "logo_material": "CL",
    "logo_material_name": "f8D1f0A2eC",
    "color": "BLA",
    "color_name": "B6b6D5f5c4",
    "logo_color": "WHI",
    "logo_color_name": "d7D2F5b0c1",
    "size": "XL",
    "size_name": "E48fB1b5F2",
  }
]

but instead im getting null instead of random values in the '_name" properties

If i hardcode the json in the code its work as expected so i assume im doing a mistake in the way i handle the input json.

Thanks in advance.

Try this, it uses the loop structure suggested by the code node. Not sure why you’d need to make a copy with JSON parse/stringify:

// Function to generate random values
function getRandomValue() {
  const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  let result = '';

  for (let i = 0; i < 10; i++) {
    const randomIndex = Math.floor(Math.random() * characters.length);
    result += characters.charAt(randomIndex);
  }

  return result;
}

// Loop over the input
for (let item of $input.all()) {
  for (let key in item.json) {
    if (key.endsWith("_name")) {
      item.json[key] = getRandomValue();
    }
  }
}

return $input.all();
3 Likes

EDIT

If you try to access all items of a specific node, like this in our situation $("NODE1").all() it fails but with $input.all() it works as intented.

In my scenario i have to name the input node and avoid the $input.all() .

Is that possible ?

Jon told me me that the Code node expects only one input so my Scenario cant works directly, as i cant access the $input.all() form two different nodes.

Also as the output of the nodes is completely different i also cant use merge. So i dont know how this can be done.

Can you provide a workflow that better represents your end state with the two sets of inputs? It doesn’t need to work, but define what’s coming in, where, and what the output would be.

This problem doesn’t sound like it’s out of scope for n8n

Yes i can do that, I have to apologise because i didnt knew how to add mock data in the first place so here you are

What i want is to fill all the ‘_name’ properties of the Main Node JSON based if the Data is found in Extra Node.

For example is the Extra Node has an ITEM that its ‘abbr’ ( let say abbr: WHI ) matches a property value of the Main Node ( let say that Main Node color: WHI ) then to fill the _name property equivelant ( color_name in this exmple ) with the attribute_value of the Extra Node ( White in this example )

There are many _name proprties color is just one of them. In the workflow i have mock data with wanted results.

I hope it is more clear now.

Thanks in advance.

Righto, I get it. We can’t grab that output from the extra node unless it’s connected.

This puts all that extra info into a single item in the data field. It then concatenates that with your main data, and in the code node pops off the lookup to use against the rest of the items:

2 Likes

ohhh

The Concatenate —> Merge —> Pop() did the trick to carry 2 jsons !

Thank you :smiley:

Its a nice and clever “hackery” but wouldn’t be better is the nodes can accept multiple nodes in the future ?

Indeed, a globally available lookup table mechanism would be great per workflow. You could always pop this into staticData and avoid this whole merge/concat/pop thing, but you’ll need to ensure it stays up to date. Also not feasible if the lookup data is huge as staticData is stored with the workflow, so every workflow load is going to be impacted.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.