SH4444KO
SH4444KO

Reputation: 191

Dynamically trying to update a field in an object using dataweave, how to achieve the same?

The use case is : we'll be having translations stored in an object in a variable in the dataweave, we have to dynamically assign those translations in the "calingaKey" value, I tried doing something below, but it isn't working.

Input

{
    "editable": true,
    "sections": [
        {
            "title": "Identification",
            "calingaKey": "",
            "content": [
                {
                    "name": "Classification",
                    "text": "Product",
                    "url": "",
                    "info": ""
                },
                {
                    "name": "Product Number",
                    "text": "####1234",
                    "url": "",
                    "info": ""
                }
            ]
        },
        {
            "title": "Position and Contact",
            "calingaKey": "",
            "content": [
                {
                    "name": "Manufacturer",
                    "text": "Value of Manufacturer",
                    "url": "",
                    "info": ""
                },
                {
                    "name": "Hardware Version",
                    "text": "####1234",
                    "url": "",
                    "info": ""
                }
            ]
        }
    ]
}

Dataweave

%dw 2.0
import * from dw::util::Values
output application/json
var calinga = {
    "title": "titel",
    "Position and contact": "Lage und Kontakt"
}
---
payload.sections map (item,index)->(item mapObject ((value, key, index) -> {'$(key)' : value} update field("calingaKey") with calinga.'$(key)' ))

"calinga.'$(key)'" doesn't seem to work for some reason, and gives me null, any issue with my code?

Expected Output

[
  {
    "title": "Identification",
    "calingaKey": "titel",
    "content": [
      {
        "name": "Classification",
        "text": "Product",
        "url": "",
        "info": ""
      },
      {
        "name": "Product Number",
        "text": "####1234",
        "url": "",
        "info": ""
      }
    ]
  },
  {
    "title": "Position and Contact",
    "calingaKey": "titel",
    "content": [
      {
        "name": "Manufacturer",
        "text": "Value of Manufacturer",
        "url": "",
        "info": ""
      },
      {
        "name": "Hardware Version",
        "text": "####1234",
        "url": "",
        "info": ""
      }
    ]
  }
]

Upvotes: 0

Views: 1006

Answers (1)

Salim Khan
Salim Khan

Reputation: 4303

%dw 2.0
import * from dw::util::Values
output application/json
var calinga = {
    "title": "titel1",
    "calingaKey": "titel1",
    "Position and contact": "Lage und Kontakt"
}
---
payload.sections map (item,index)->(item mapObject ((value, key, index) -> {'$(key)' : value} update field("calingaKey") with calinga[key]

    ))

Upvotes: 0

Related Questions