4

I am having a JSON like this :

{
  "dcsId": "1184001100000000517",
  "marketCode": "US",
  "languageCode": "en-US",
  "profile": {
  "base": {
     "username": "arunima27",
     "activeInd": "R",
     "phone": [
       {
          "activeInd": "Y",
          "type": "mobile",
          "primaryInd": "Y",
          "number": "2234566788"
       },
       {
         "activeInd": "N",
         "type": "mobile",
         "primaryInd": "N",
         "number": ""
       }
      ]
    }
  }
 }

From this input JSON we need to extract the payload.profile.base.phone.number where the payload.profile.base.phone.type == "mobile" and payload.profile.base.phone.activeInd == "Y". Actually we need to loop through the JSON array (payload.profile.base.*phone) and get only the phone numbers which are active and having the category / type as mobile.

We need the output like below :

{
  "dcsId": "1184001100000000517",
  "marketCode": "US",
  "languageCode": "en-US",
  "username" :  "arunima27", 
  "phoneNumber" : "2234566788"
}  

We are facing problem in doing this transformation for the "phoneNumber" output variable.

Mohit Mehrotra
  • 201
  • 1
  • 6
  • 12

4 Answers4

9

from your expected result it looks like you only want the first matching number. here is the dataweave doing exactly this:

%dw 1.0
%output application/json
---
{
    dcsid: payload.dcsId,
    markCode: payload.marketCode,
    languageCode: payload.languageCode,
    username: payload.profile.base.username,
    phoneNumber: (payload.profile.base.phone filter ($.activeInd == 'Y' and $.type == 'mobile'))[0].number
}

if you want all matching phone numbers, just leave the [0] out and the value of phoneNumber in the result will be a array (not just the first matching phone number).

Yevgeniy
  • 2,614
  • 1
  • 19
  • 26
4

This has solved the problem.

{(payload.profile.base.phone filter ($.activeInd == "Y" and $.primaryInd== "Y"))}.number default ""

Mohit Mehrotra
  • 201
  • 1
  • 6
  • 12
1

Use filter and map on phone to achieve the desired result.

phoneNumber: {
    (payload.profile.base.phone filter ($.activeInd == "Y" and $.type == "mobile") map {
        number: $.number
    }
)}

Output

"phoneNumber": {
    "number": "2234566788",
 }
Abhay
  • 314
  • 1
  • 2
  • 11
  • nope, phoneNumber won't be a array. In case of multiple matching phone numbers your dataweave will produce invalid JSON. phoneNumber will be a object containing to attributes with key `number` – Yevgeniy Apr 19 '17 at 10:39
-2
 %dw 2.0
 output application/json
 ---
 payload.profile.base.phone filter ((obj, index) -> (obj.activeInd == 'Y' and obj.type  ~= 'mobile'   ))