Dataweave转换:修改JSON值

时间:2017-11-13 12:07:26

标签: json mule dataweave

我想进行下面的dataweave转换。 " SourceValue"应该像 "来源"和" SrcVal"在输出有效载荷中。你能建议怎么做。

输入:

{       "payload":[
      {
         "key":"stage",
         "value":"Completed"
      },
      {
         "key":"url",
         "value":"abc.com"
      },
      {
         "key":"SourceValue",
         "value":"abc"
      }
   ]
}

输出:

{
   "part1":[
      {
         "key":"stage",
         "value":"Completed"
      },
      {
         "key":"url",
         "value":"abc.com"
      },
      {
         "key":"Source",
         "value":"abc"
      }
   ],
   "part2":[
      {
         "key":"url",
         "value":"abc.com"
      },
      {
         "key":"SrcVal",
         "value":"abc"
      }
   ]
}

TIA

1 个答案:

答案 0 :(得分:0)

使用以下内容:

%dw 1.0
%input payload application/json
%output application/json
%var a=2
---
{
    part1: payload.payload map ((payload01 , indexOfPayload01) -> {
        "key" : "Source" when payload01.key == "SourceValue" otherwise payload01.key,
        "value" : payload01.value

    }),
     part2: payload.payload filter ($.key == "url"  or $.key == "SourceValue") map ((payload01 , indexOfPayload01) ->  
         {
         "key" : "SrcVal" when payload01.key == "SourceValue" otherwise payload01.key,
         "value" : payload01.value  
         }
     ) 

}