Mule ESB dataweave如何忽略null

问题描述 投票:0回答:2

在mule ESB dataweave中,我无法忽略空对象,{}。我正在尝试检查输入中是否存在特定的表。如果它存在,我会做一些业务逻辑,如果它不存在,它不应该包含在输出中。但是,我得到{}而不是什么。

这是我的输入文件:

{
  "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"
              }
            ]
          }
        ]
      }   
    ]
  }
}

这是我的数据编织:

%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
          })
        })
      })
    }
  ]
}

这个输出文件:

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

任何人都可以建议如何摆脱空物,{},拜托?

谢谢你,并问及NK

mule esb dataweave
2个回答
0
投票

最简单的方法是删除最后的所有空元素,如下所示:

%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) }

这输出:

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

0
投票

有了Josh的帮助,如果有兴趣的话,这就是解决方案。使用过滤器组合的大小

%dw 1.0

%输出应用程序/ json skipNullOn =“一切”

{
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]}
© www.soinside.com 2019 - 2024. All rights reserved.