1

I am unable to transform the below input payload in json to expected output. Below is the input I got, and the mapping which I tried, I have to ignore when the value of bea_order is blank which can we done with the filter, however my requirement is to pickup all the values from inside the array and club them together with values from other arrays.

Input:

[
   {
      "bea_order":[
         
      ]
   },
   {
      "bea_order":[
         {
            "PO_Receipt_Date__c":"2021-08-13",
            "Id":"a3I1T000003E6SQUA0"
         },
         {
            "PO_Receipt_Date__c":null,
            "Id":"a3Iq0000000OhnREAS"
         },
         {
            "PO_Receipt_Date__c":null,
            "Id":"a3Iq0000000OhnWEAS"
         }
      ]
   },
   {
      "bea_order":[
         {
            "PO_Receipt_Date__c":"2022-05-06",
            "Id":"a3I1T000003E6SOUA0"
         }
      ]
   },
   {
      "bea_order":[
         
      ]
   }
]

Output:

[
   {
      "id":"a3I1T000003E6SQUA0",
      "flag":"false"
   },
   {
      "id":"a3Iq0000000OhnREAS",
      "flag":"false"
   },
   {
      "id":"a3Iq0000000OhnWEAS",
      "flag":"false"
   },
   {
      "id":"a3I1T000003E6SOUA0",
      "flag":"false"
   }
]

Mapping I tried:

 %dw 2.0
output application/json
---
payload.bea_order filter ($.Id != null) map () -> {
    id : $.Id,
    flag : 'false'
}

How to get the expected output?

5 Answers 5

1

You could try with this approach:

%dw 2.0
output application/json
---
flatten(payload..bea_order - []) map {
    id: $.Id,
    flag: false
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try with flatMap() instead of map().

Comments

0

I think this will do the trick:

%dw 2.0
output application/json
---
payload filter ((item, index) -> sizeOf(item.bea_order) > 0) 
        flatMap ((item, index) -> (item.bea_order)) 
        map ((order, index) -> {
    "flag": false,
    "id" : order.Id
})

This is the output I get

[
  {
    "flag": false,
    "id": "a3I1T000003E6SQUA0"
  },
  {
    "flag": false,
    "id": "a3Iq0000000OhnREAS"
  },
  {
    "flag": false,
    "id": "a3Iq0000000OhnWEAS"
  },
  {
    "flag": false,
    "id": "a3I1T000003E6SOUA0"
  }
]

Basically, first I filter out the elements with empty bea_order, then flatMap the results and map them to their new element shape (with the 'flag' attribute)

Comments

0

Step by step :

%dw 2.0
output application/json
---
payload map $.bea_order                 // get bea-order content  
        flatMap $                       // flatten arrays and remove empty ones 
        map {                           // transform to wanted result
          ($ - "PO_Receipt_Date__c"),
          "flag": false
        }

Comments

0

Apart from merging the sub arrays, flatten also does the trick of omitting the empty sub arrays. Hope this helps.

%dw 2.0
output application/json
---
flatten(payload.bea_order) map {
    "id": $.Id,
    "flag":"false"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.