0

我有这个作为输入:"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"
}
]

有任何想法吗?

4

2 回答 2

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"
        }
      ]
    }
于 2016-01-24T14:30:53.460 回答
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"
}
于 2016-01-26T03:36:54.010 回答