Go through node transformer and insert value from root attributes

I have these input data

{
“variants”: {
“variant”: [
{
“by-lang”: {
“data”: [
{
“name”: “A-board A1 Complete Set Bread English”,
@lang”: “eu”,
“description”: “

  • Complete A-Board with print design
  • Standard print design
  • A1 format Premium Poster Paper 135g/m²
  • Suitable for indoor and covered outdoor areas

},
{
“name”: “A-board A1 Complete Set Bread English”,
@lang”: “es”
},
{
“name”: “A-board A1 Complete Set Bread English”,
@lang”: “cz”,
“description”: “
  • CZ Complete A-Board with print design
  • Standard print design
  • A1 format Premium Poster Paper 135g/m²
  • Suitable for indoor and covered outdoor areas

}
]
}
},
{
“by-lang”: {
“data”: [
{
“name”: “A-board A1 Complete Set Bread English”,
@lang”: “cz”
}
]
}
}
]
},
“common-data”:{
“by-lang”: {
“data”: [
{
@lang”: “cz”,
“description”: “cz default desc”
},
{
@lang”: “es”,
“description”: “es default desc”
}
]
}
}
}

i want to insert “use_description” into “variants.variant..by-lang.data..” and value should be from “variants.variant..by-lang.data..description” and if it does not exist.
Insert default value from “common-data.by-lang.data.*.description” by its “@lang”.

Thankx

Hi @vosy,

Could you please confirm whether you expect the following JSON as the output?

{
    "variants": {
        "variant": [
            {
                "by-lang": {
                    "data": [
                        {
                            "name": "A-board A1 Complete Set Bread English",
                            "@lang": "eu",
                            "description": "Complete A-Board with print design ..."
                            "use_description": "Complete A-Board with print design ..."
                        },
                        {
                            "name": "A-board A1 Complete Set Bread English",
                            "@lang": "es",
                            "use_description": "es default desc"
                        },
                        {
                            "name": "A-board A1 Complete Set Bread English",
                            "@lang": "cz",
                            "description": "CZ Complete A-Board with print design ..."
                            "use_description": "CZ Complete A-Board with print design ..."
                        }
                    ]
                }
            },
            {
                "by-lang": {
                    "data": [
                        {
                            "name": "A-board A1 Complete Set Bread English",
                            "@lang": "cz"
                            "use_description": "cz default desc"
                        }
                    ]
                }
            }
        ]
    },
    "common-data": {
        "by-lang": {
            "data": [
                {
                    "@lang": "cz",
                    "description": "cz default desc"
                },
                {
                    "@lang": "es",
                    "description": "es default desc"
                }
            ]
        }
    }
}

wauuuuu, it looks looks healthy :star_struck:

thank you very much! Gugi, but what if common-data.by-lang.data.*.description is empty or does not exists then use variants.variant.by-lang.data.head.description and if this does not exists use string “Description does not exists”

{
“variants”: {
“variant”: [
{
“by-lang”: {
“data”: [
{
“name”: “A-board A1 Complete Set Bread English”,
@lang”: “eu”,
“description”: “Complete A-Board with print design …”
},
{
“name”: “A-board A1 Complete Set Bread English”,
@lang”: “es”,
},
{
“name”: “A-board A1 Complete Set Bread English”,
@lang”: “cz”,
“description”: “CZ Complete A-Board with print design …”
}
]
}
},
{
“by-lang”: {
“data”: [
{
“head”:{
“title”:“A-board A1 stojan s tiskem pla …”
“keywords”:“A-board A1 Complete Set Coffee”
“description”:“head description”
},
“name”: “A-board A1 Complete Set Bread English”,
@lang”: “cz”
}
]
}
}
]
},
“common-data”: {
“by-lang”: {
“data”: [
{
@lang”: “cz”,
“description”: “”
},
{
@lang”: “es”,
“description”: “es default desc”
}
]
}
}
}

it will be great

Hi @vosy,

It is very possible. Please find the steps you should do to achieve the objective.

For example, here is the original JSON:

{
    "variants": {
        "variant": [
            {
                "by-lang": {
                    "data": [
                        {
                            "name": "A-board A1 Complete Set Bread English",
                            "@lang": "eu",
                            "description": "Complete A-Board with print design ..."
                        },
                        {
                            "name": "A-board A1 Complete Set Bread English",
                            "@lang": "es"
                        },
                        {
                            "name": "A-board A1 Complete Set Bread English",
                            "@lang": "cz",
                            "description": "CZ Complete A-Board with print design ..."
                        }
                    ]
                }
            },
            {
                "by-lang": {
                    "data": [
                        {
                            "head": {
                                "title": "A-board A1 stojan s tiskem pla ...",
                                "keywords": "A-board A1 Complete Set Coffee",
                                "description": "head description"
                            },
                            "name": "A-board A1 Complete Set Bread English",
                            "@lang": "pt"
                        },
                        {
                            "name": "A-board A1 Complete Set Bread English",
                            "@lang": "cz"
                        }
                    ]
                }
            }
        ]
    },
    "common-data": {
        "by-lang": {
            "data": [
                {
                    "@lang": "cz",
                    "description": ""
                },
                {
                    "@lang": "es",
                    "description": "es default desc"
                }
            ]
        }
    }
}

First, we should copy the common-data.by-lang.data array to each variant data using Recursively copy values to children. This will allow each variant data to access common data since a deeper object in an array can’t access values from the higher level directly. The common data will be available as default_lang.

Then, we should loop through the variant data using Execute entity transformers and Node, transform nodes. This allows us to put transformers that will apply to each object separately in the array.

Inside the Node, transform nodes, put another Recursively copy values to children to copy the @lang to each lang in the common data array for filtering purposes later. The selected lang will be available as use_lang on each common lang.

Still inside the same Node, transform nodes, put a Value Setter to set the use_description property using JMESPath.

&{description || (default_lang[?"@lang" == use_lang].description | [0]) || head.description || 'Description does not exist'}

Finally, you can remove the temporary default_lang property from each variant data using Key Filter.

Below is the result of the transformations.

{
  "variants": {
    "variant": [
      {
        "by-lang": {
          "data": [
            {
              "name": "A-board A1 Complete Set Bread English",
              "@lang": "eu",
              "description": "Complete A-Board with print design ...",
              "use_description": "Complete A-Board with print design ..."
            },
            {
              "name": "A-board A1 Complete Set Bread English",
              "@lang": "es",
              "use_description": "es default desc"
            },
            {
              "name": "A-board A1 Complete Set Bread English",
              "@lang": "cz",
              "description": "CZ Complete A-Board with print design ...",
              "use_description": "CZ Complete A-Board with print design ..."
            }
          ]
        }
      },
      {
        "by-lang": {
          "data": [
            {
              "head": {
                "title": "A-board A1 stojan s tiskem pla ...",
                "keywords": "A-board A1 Complete Set Coffee",
                "description": "head description"
              },
              "name": "A-board A1 Complete Set Bread English",
              "@lang": "pt",
              "use_description": "head description"
            },
            {
              "name": "A-board A1 Complete Set Bread English",
              "@lang": "cz",
              "use_description": "Description does not exist"
            }
          ]
        }
      }
    ]
  },
  "common-data": {
    "by-lang": {
      "data": [
        {
          "@lang": "cz",
          "description": ""
        },
        {
          "@lang": "es",
          "description": "es default desc"
        }
      ]
    }
  }
}

We hope our explanation is clear to you. Feel free to let us know if you find any issues.

I’m going to study it and learn something new, thank you.

1 Like

Hi Gugi, works perfectly, thank you very much! a lot :+1:

1 Like

Hi @vosy, I’m glad to hear that it worked perfectly!