DataWeave Transformer帮助编码

时间:2016-01-05 16:37:38

标签: dataweave

我将此作为输入:"keys":[{"key":"key1","value":"testKey1"},{"key":"Key2","value":"testKey2"}]

我要做的是以json格式获取此输出:

   { "key1": "testKey1",
     "key2":testKey2}

我正在尝试使用mule中的DataWeave转换器:

 %dw 1.0
 %output application/json skipNullOn="everywhere"
 ---
{
price: payload.price,
"": payload.keys filter $.value !=null   
} 

但它显示了这个:

"": [
{
  "value": "key1",
  "key": "testKey1"
},
{
  "value": "key2",
  "key": "testKey12"
}
]

任何想法?

2 个答案:

答案 0 :(得分:0)

我使用了以下消息片段

    {
        "keys":
        [
            {
                "key":"key1",
                "value":"testKey1"},
            {
                "key":"Key2"
            }
        ]
    }

我使用下面的Dataweave进行了转换

%dw 1.0
%output application/json skipNullOn="everywhere"
---
{
    keys: payload.keys map {
        key: $.key,
        value: $.value
    }
}

回复如下。当值不可用时,该值不会出现在响应中

    {
      "keys": [
        {
          "key": "key1",
          "value": "testKey1"
        },
        {
          "key": "Key2"
        }
      ]
    }

答案 1 :(得分:0)

这应该有效:

<强>输入:

{
  "keys":[
    {
     "key":"key1",
     "value":"value1"
    },
    {
     "key":"key2",
     "value":null
    },
    {
     "key":"key3",
     "value":"value3"
    }
  ]
}

Dw脚本:

%dw 1.0
%input payload application/json
%output application/json
---
{
  (payload.keys filter $.value !=null map {
     ($.key): $.value
  })
}

<强>输出:

{
  "key1": "value1",
  "key3": "value3"
}