Lookup in two objects and match by key

Hello,

I’m trying to copy/move a value from one object to another when they share the same key.

For example this is my data:

{
  "metafields": [
    {
      "id": null,
      "key": "kilometers",
      "type": "number_integer",
      "value": "15",
      "namespace": "custom"
    },
    {
      "id": null,
      "key": "model",
      "type": "single_line_text_field",
      "value": "A1",
      "namespace": "custom"
    }
  ],
  "shopifyProduct": {
    "metafields": {
      "nodes": [
        {
          "id": "gid://shopify/Metafield/36311756865816",
          "key": "model",
          "type": "single_line_text_field",
          "value": "A1",
          "namespace": "custom"
        },
        {
          "id": "gid://shopify/Metafield/36312200544536",
          "key": "kilometers",
          "type": "number_integer",
          "value": "25",
          "namespace": "custom"
        },
        {
          "id": "gid://shopify/Metafield/36312579801368",
          "key": "transmissie",
          "type": "single_line_text_field",
          "value": "Automaat",
          "namespace": "custom"
        }
      ]
    }
  }
}

When a key from “metafields” and “shopifyProduct.metafields.nodes” match, the value from "“shopifyProduct.metafields.nodes” should be copied to the corresponding object in “metafields”.

The above example should result in:

{
  "metafields": [
    {
      "id": "gid://shopify/Metafield/36312200544536",
      "key": "kilometers",
      "type": "number_integer",
      "value": "15",
      "namespace": "custom"
    },
    {
      "id": "gid://shopify/Metafield/36311756865816",
      "key": "model",
      "type": "single_line_text_field",
      "value": "A1",
      "namespace": "custom"
    }
  ],
  "shopifyProduct": {
    "metafields": {
      "nodes": [
        {
          "id": "gid://shopify/Metafield/36311756865816",
          "key": "model",
          "type": "single_line_text_field",
          "value": "A1",
          "namespace": "custom"
        },
        {
          "id": "gid://shopify/Metafield/36312200544536",
          "key": "kilometers",
          "type": "number_integer",
          "value": "25",
          "namespace": "custom"
        },
        {
          "id": "gid://shopify/Metafield/36312579801368",
          "key": "transmissie",
          "type": "single_line_text_field",
          "value": "Automaat",
          "namespace": "custom"
        }
      ]
    }
  }
}

We cannot trust on the order of the items in both objects because they can be dynamic. That is why we need to check on the key, that should be the same.

Thanks in advance!

Hi @yoeri

Here is one example to fix this.
Try importing it and have a look.

I hope it helps you!

testing-transformer_transformer.ndjson (1015 Bytes)

1 Like

Hi @mko,

Thank you for your reply. It does exactly what I want :slight_smile: