我在请求正文中有以下JSON。
{
"Type": "Person",
"IndividualId": "acd47048-bd80-476e-aa33-741bd5cb05d3",
"Attributes": [
{"Name":"FirstName","Value":"John"},
{"Name":"LastName","Value":"Stewart"},
{"Name":"Company","Value":"ABC"}
{"Name":"Country","Value":"United States"}
]
}
我想要的是将属性提取为命名字段,如:
{
"FirstName"="John",
"LastName"="Stewart",
"Company"="ABC",
"Country"="United States"
}
无论如何迭代它并在Dataweaver或其他任何方式中提取命名属性?此请求正文可以包含任何数量的属性,不限于示例中的属性。
答案 0 :(得分:0)
您可以使用dataweave对输入有效负载进行操作。输出可以根据您的要求为json或java。以下代码应该可以工作。
<dw:transform-message doc:name="Transform Message">
<dw:input-payload mimeType="application/json"/>
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{(payload.Attributes map {
($.Name) : $.Value
})}]]>
</dw:set-payload>
</dw:transform-message>
希望这有帮助。
答案 1 :(得分:0)
只需使用for loop
,如下所示: -
的 Soultion1 强>:
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="testFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/store" doc:name="HTTP"/>
<expression-component doc:name="Expression">
<![CDATA[
StringBuffer sb = new StringBuffer();
flowVars.stBuffer=sb;
]]>
</expression-component>
<set-variable variableName="counter" value="1" doc:name="Variable"/>
<json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object"/>
<foreach collection="#[payload.Attributes]" doc:name="For Each">
<expression-component doc:name="Expression">
<![CDATA[
if(Integer.parseInt(flowVars.counter)>1)
{
flowVars.stBuffer.append(","+'"'+payload.Name+'" = "'+payload.Value+'"').toString()
}
else
{
flowVars.stBuffer.append('"'+payload.Name+'" = "'+payload.Value+'"').toString()
}
flowVars.counter= flowVars.counter +1;
]]>
</expression-component>
</foreach>
<set-payload value="{#[flowVars.stBuffer.toString()]}" doc:name="Set Payload"/>
</flow>
解决方案2: Dataweave
<flow name="testtFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/store" doc:name="HTTP"/>
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
//This will iterate the value
{
(payload.Attributes map {
($.Name) : $.Value
})
}]]></dw:set-payload>
</dw:transform-message>
<object-to-string-transformer doc:name="Object to String"/>
</flow>
输出将是: - {"FirstName" = "John","LastName" = "Stewart","Company" = "ABC","Country" = "United States"}