Copy data between each other by keys

Hello everyone,

Problem

{
 "products":[
           (object),
           (object)
     ]
}


{
 "prices":[
           (object),
           (object)
     ]
}

Solution


{
 "products":[
           {
           ...
           ...
          (pricesObject1)
           },
           {
           ...
           ...
          (pricesObject2)
           }
     ]
}

Your post is not entirely clear to me, but it reminds me of a shared configuration.
Maybe this configuration uses some tactics that you need?

Let me rephrase then

I possess the following object and need to remove any elements(wProductVariations.attributes) from wProductVariations that do not match the attributes object at the root level:

{
  "attributes": [
    {
      "id": 1,
      "option": "SMALL"
    },
    {
      "id": 2,
      "option": "White"
    }
  ],
  "wProductVariations": [
    {
      "id": 2237,
      "attributes": [
        {
          "id": 1,
          "option": "XX-Large"
        },
        {
          "id": 2,
          "option": "White"
        }
      ],
      "menu_order": 0,
      "meta_data": [
        {
          "id": 56685,
          "key": "_wc_additional_variation_images",
          "value": ""
        }
      ]
    },
    {
      "id": 2236,
      "attributes": [
        {
          "id": 1,
          "option": "X-Large"
        },
        {
          "id": 2,
          "option": "White"
        }
      ]
    },
    {
      "id": 2235,
      "attributes": [
        {
          "id": 1,
          "option": "Large"
        },
        {
          "id": 2,
          "option": "White"
        }
      ]
    },
    {
      "id": 2234,
      "attributes": [
        {
          "id": 1,
          "option": "Medium"
        },
        {
          "id": 2,
          "option": "White"
        }
      ]
    },
    {
      "id": 2233,
      "attributes": [
        {
          "id": 1,
          "option": "Small"
        },
        {
          "id": 2,
          "option": "White"
        }
      ]
    }
  ]
}

hope this is clear now? Thanks in advance

@Alumio_support
I-am a bit busy, seems like it can be done with JMESpath perhaps?
Could you help out?

alright i will check it out

Hi @abubakar

You can recursively copy the root level’s attributes object into all the objects of wProductVariantions array as match_attributes. Then you can use the below JMESPath to filter out all objects that do not match the attributes object copied inside each wProductVariants objects.

&{wProductVariations[?attributes == match_attributes]}

Please give it a try and let us know if you have any questions.

1 Like

Thanks @Gugi and @anon99371720, this solution works fine