简体   繁体   中英

Mule Dataweave null check and array empty check of JSON payloads

Need to check null and empty array in Dataweave using filter or any other logic.

First check the parentId is null, we need to skip the particular JSON. If parentId is not null need to check the mobileContacts or emailContacts have the array of values. If mobileContacts and emailContacts have empty List we need skip the particular JSON values. If any one have the value we need to process the records.

Input JSON:

{
  "name": "XYZ",
  "age": 23, 
  "results": [
    {
      "parentId": "12345",      
      "notes": "proceed",
      "mobileContacts": [
        {
          "relationId": "12345",
          "callId": "3456213"
        },
        {
          "relationId": "12345",
          "callId": "12345"
        }
      ],
      "emailContacts": [ ],
      "initial": true
    },
     {
      "parentId": "435638",      
      "notes": "proceed",
      "mobileContacts": [ ],
      "emailContacts": [ ],
      "initial": true
    },
     {
      "parentId": null,      
      "notes": "proceed",
      "mobileContacts": [
        {
          "relationId": "12345",
          "callId": "3456213"
        },
        {
          "relationId": "12345",
          "callId": "12345"
        }
      ],
      "emailContacts": [ ],
      "initial": true
    }
  ]
}

Need below Output:

{
  "name": "XYZ",
  "age": 23, 
  "results": [
    {
      "parentId": "12345",      
      "notes": "proceed",
      "mobileContacts": [
        {
          "relationId": "12345",
          "callId": "3456213"
        },
        {
          "relationId": null,
          "callId": null
        }
      ],     
      "initial": true
    }
  ]
}

How about this,

%dw 1.0
%output application/json
---
{
    name: payload.name,
    age: payload.age, 
    results: payload.results filter ($.parentId != null and ((sizeOf $.mobileContacts) > 0 or  (sizeOf $.emailContacts) > 0))
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM