简体   繁体   中英

Mule ESB dataweave how to ignore null

In mule ESB dataweave I am having trouble ignoring empty objects, {} . I am trying to check if a particular table exists in my input. If it exists, I do some business logic, if it doesn't exist, it should not be included in the output. However, I am getting {} instead of nothing.

This is my input file :

{
  "srcTable": {
    "srcList": [
      {
        "tableNames": "table1",
        "src": [
          {
            "srcKey": [
              {
                "key": "date",
                "value": "01/01/2016"
              },
              {
                "key": "withinYearTotalMaxSection",
                "value": "2500"
              },
              {
                "key": "previousClaimsTotalMaxSection",
                "value": "25000"
              },
              {
                "key": "previousClaimsTotalMax",
                "value": "50000"
              }
            ]
          }
        ]
      },
      {
        "tableNames": "table2",
        "src": [
          {
            "srcKey": [
              {
                "key": "date",
                "value": "01/01/2016"
              },
              {
                "key": "type",
                "value": "A"
              },
              {
                "key": "garden",
                "value": "1000"
              },
              {
                "key": "risk",
                "value": "50000"
              }
            ]
          },
          {
            "srcKey": [
              {
                "key": "date",
                "value": "01/01/2016"
              },
              {
                "key": "type",
                "value": "B"
              },
              {
                "key": "garden",
                "value": "0"
              },
              {
                "key": "risk",
                "value": "50000"
              }
            ]
          }
        ]
      },
      {
        "tableNames": "table3",
        "src": [
          {
            "srcKey": [
              {
                "key": "date",
                "value": "01/01/2016"
              },
              {
                "key": "type",
                "value": "GLD"
              },
              {
                "key": "plants",
                "value": "1500"
              },
              {
                "key": "theft",
                "value": "3000"
              }
            ]
          },
          {
            "srcKey": [
              {
                "key": "date",
                "value": "01/01/2016"
              },
              {
                "key": "type",
                "value": "SVR"
              },
              {
                "key": "plants",
                "value": "0"
              },
              {
                "key": "theft",
                "value": "1000"
              }
            ]
          }
        ]
      }   
    ]
  }
}

This is my dataweave:

%dw 1.0
%output application/json skipNullOn="everything"
---
{ 
  singlevalue: [
    {
      (payload.srcTable.srcList filter ($.tableNames == 'table1') map (r,pos)-> {
        (r.src map {
          ($.srcKey filter ($.key == 'date') map {
            name: 'date',
            value: $.value
          })
        })
      })
    },
    {
      (payload.srcTable.srcList filter ($.tableNames != null and $.tableNames == 'xxx') map (r,pos)-> {
        (r.src map {
          ($.srcKey filter ($.key == 'date') map {
            name: 'date' when $.value != null otherwise null,
            value: $.value
          })
        })
      })
    }
  ]
}

This output file :

{  
  "singlevalue": [
    {
      "name": "date",
      "value": "01/01/2016"
    },
    {}
  ]
}

Can anyone suggest how to get rid of the empty objects, {} , please?

Thank you and regards NK

The easiest thing to do would be to remove all the empty elements at the end like this:

%dw 1.0
%output application/json skipNullOn="everything"

%var transformation = [
    {
      (payload.srcTable.srcList filter ($.tableNames == 'table1') map (r,pos)-> {
        (r.src map {
          ($.srcKey filter ($.key == 'date') map {
            name: 'date',
            value: $.value
          })
        })
      })
    },
    {
      (payload.srcTable.srcList filter ($.tableNames != null and $.tableNames == 'xxx') map (r,pos)-> {
        (r.src map {
          ($.srcKey filter ($.key == 'date') map {
            name: 'date' when $.value != null otherwise null,
            value: $.value
          })
        })
      })
    }
  ]

%function removeEmptyObjects(e)
  e filter $ != {}
---
{ singleValue: removeEmptyObjects(transformation) }

This outputs:

{
  "singleValue": [
    {
      "name": "date",
      "value": "01/01/2016"
    }
  ]
}

With Josh's help this is the solution if anyone interested. Using the size Of with the filter combination

%dw 1.0

%output application/json skipNullOn="everything"

{
singlevalue: [
({
    (payload.srcTable.srcList filter ($.tableNames == 'table1') map (r,pos)-> {
        (r.src map {
            ($.srcKey filter ($.key == 'date') map {
                name: 'date',
                value: $.value
            })
        })
    })
})  when (sizeOf (payload.srcTable.srcList filter $.tableNames == 'table1')) != 0,
({
    (payload.srcTable.srcList filter ($.tableNames != null and $.tableNames == 'xxx') map (r,pos)-> {
        (r.src map {
            ($.srcKey filter ($.key == 'date') map {
                name: 'date' when $.value != null otherwise null,
                value: $.value
            })
        })
    })
}) when (sizeOf (payload.srcTable.srcList filter $.tableNames == 'xxx')) != 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