1

I'm setting up a migration from an URL with JSON on it.

Got the first level values without any issues. Currently I'm stuck at nested arrays which resemble something like this:

{   "content": [
    {
      "id": 148,
      "hardwares": [
        {
          "hwId": 16,
          "products": [
            {
              "pid": 2097,
              "productName": "ProductName1",
            },
            {
              "pid": 2100,
              "productName": "ProductName2",
            },
            {
              "pid": 2103,
              "productName": "ProductName3",
            },
            {
              "pid": 2094,
              "productName": "ProductName4",
            }
          ]
        }
      ]
    },
   ], 
}

I have a custom content entity with a text field called Product Names, the field is a multi-value field. I've been trying to get the productName values and populate the field. Basically this JSON here would create one entity, and then get the value by this hierarchy:

hardwares->products->productName

I can't seem to grasp the usage of the sub_process plugin and I would really appreciate any hints or any working examples.

0

What I did:

I defined a field entry in the source:

  name: hardwares
  label: 'Hardwares'
  selector: hardwares

In my process:

_products:
    plugin: sub_process
    source: hardwares
    process:
      products:
        plugin: sub_process
        source: products
        process:
          productName: productName
field_products:
    plugin: flatten
    source: '@_products'
_hwIds:
    plugin: sub_process
    source: hardwares
    process:
      hwId: hwId
field_hardware_ids:
    plugin: flatten
    source: '@_hwIds'

In order to get to the 2nd level of my data array, I needed to nest the sub_process plugin. I flatten the result afterwards in order to get a numerically indexed array which resembles the multiple value field structure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.