Forum Discussion

aditya_gupta41's avatar
aditya_gupta41
Contributor
4 years ago
Solved

Changing keys of an Object

Hello Experts,

I have a requirement where I need to change the value of one single key in an object.

INPUT:

[
  {
    "soapenv:Envelope": {
      "@xmlns:soapenv": "http://schemas.xmlsoap.org/soap/envelope/",
      "@xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
      "@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
      "soapenv:Body": {
        "ns1:MT_SWE_FI_PEGA_ABAON": {
          "@xmlns:ns1": "http://xyz.com",
          "SYSTEMID": "AB1",
          "CLIENT": "090",
          "INPUT": {
            "S_BUKRS": "00",
            "S_ANLN1": "12345678",
            "S_ANLN2": "0000",
            "S_ZUGDT": "2022-06-20",
            "ERLBT": "100"
          }
        }
      }
    }
  }
]

Here I just need to change key ‘CLIENT’ to ‘CLIENTID’.

Thanks in Advance

  • Hi @aditya.gupta41

    you should use the function mapKeys in a mapper snap to get the key value and rename it.

    Inside the mapper snap you should use this expression:
    $[‘soapenv:Envelope’][‘soapenv:Body’][‘ns1:MT_SWE_FI_PEGA_ABAON’].mapKeys((value, key) => key == “CLIENT” ? “CLIENTID” : key)

    This expression will first check every key if it equals the value “CLIENT” and if true it will rename it to whatever you want to rename it to. If false the key name is not changed

    And give as target path the path of the original one until the object that contains CLIENT
    $[‘soapenv:Envelope’][‘soapenv:Body’][‘ns1:MT_SWE_FI_PEGA_ABAON’]

    The result:

    Regards

    Jens

2 Replies

  • Hi @aditya.gupta41

    you should use the function mapKeys in a mapper snap to get the key value and rename it.

    Inside the mapper snap you should use this expression:
    $[‘soapenv:Envelope’][‘soapenv:Body’][‘ns1:MT_SWE_FI_PEGA_ABAON’].mapKeys((value, key) => key == “CLIENT” ? “CLIENTID” : key)

    This expression will first check every key if it equals the value “CLIENT” and if true it will rename it to whatever you want to rename it to. If false the key name is not changed

    And give as target path the path of the original one until the object that contains CLIENT
    $[‘soapenv:Envelope’][‘soapenv:Body’][‘ns1:MT_SWE_FI_PEGA_ABAON’]

    The result:

    Regards

    Jens