SP-
SP-

Reputation: 37

How to get desired output for matching keys in dataweave2.0

Input

{
  "uniqueId": "jhiu78y87y",
  "appId": "SF",
  "dob": "BgYN=",
  "ssn": "Rn=",
  "dln": "ZJ4=",
  "dla": "TX",
  "dle": "2025-09-30"
}

Need to match the key along with value after that format field have to update. Format field condition would be dob=DATE, ssn=SSN and dln=NUM other than these String would be the value of format field.

Expected Output

[
  {
      "tokenReq": "rsa",
      "tokenRes": "vol",
      "data": [
          {
              "name": "dob",
              "value": "BgYN=",
              "format": "DATE"
          },
          {
              "name": "ssn",
              "value": "Rn=",
              "format": "SSN"
          },
          {
              "name": "dln",
              "value": "ZJ4=",
              "format": "NUM"
          }
          
      ]
  },
  {
      "tokenReq": "plain",
      "tokenRes": "vol",
      "data": [
          {
              "name": "dla",
              "value": "TX",
              "format": "STRING"
          },
          {
              "name": "dle",
              "value": "2023-09-30",
              "format": "STRING"
          }
      ]
  }
]

Upvotes: 1

Views: 49

Answers (1)

StackOverflowed
StackOverflowed

Reputation: 759

This is one way to do based on your input and output.

%dw 2.0
var formatFields = {
  dob: "DATE",
  ssn: "SSN",
  dln: "NUM",
  other: "STRING"
}
output application/json  
---
do {
  var rsa = payload filterObject (keysOf(formatFields) contains $$)
  var plain = payload - "uniqueId" - "appId" -- rsa
  ---
  [
    {
      "tokenReq": "rsa",
      "tokenRes": "vol",
      "data": rsa pluck {
        "name": $$,
        "value": $,
        "format": formatFields[$$]
      }
    },
    {
      "tokenReq": "plain",
      "tokenRes": "vol",
      "data": plain pluck {
        "name": $$,
        "value": $,
        "format": formatFields['other']
      }
    }
  ]
}

Upvotes: 2

Related Questions