Reputation: 4250
I have following response from a API call and now trying to get a value in a variable.
[{"Name":"My name","Address":"add1","Location":"NY"}]
Tried all following methods but all are returning null or error.
<set-variable variableName="alertIdPayload" value="#[payload[0].Name]" doc:name="Payload"/>
<set-variable variableName="alertIdPayload" value="#[message.payload[0].Name]" doc:name="Payload"/>
<set-variable variableName="alertIdPayload" value="#[json:Name]" doc:name="Payload"/>
<set-variable variableName="alertIdPayload" value="#[json:payload[0]/Name]" doc:name="Payload"/>
Any idea how to get this value?
Upvotes: 0
Views: 2889
Reputation: 11606
Convert to a Array of Maps first, then use the MEL expressions to extract the value:
<json:json-to-object-transformer
returnClass="java.util.HashMap[]" doc:name="JSON to Object" />
<set-variable variableName="alertIdPayload" value="#[payload[0].Name]" doc:name="Payload"/>
Upvotes: 1
Reputation: 2233
You can either set these in DataWeave, or do a json-to-object
transform and use the set-variable
component:
Here's dataweave:
<dw:transform-message doc:name="Transform Message">
<dw:set-variable variableName="alertIdPayload">
<![CDATA[
%dw 1.0
%output application/java
---
payload[0].Name
]]>
</dw:set-variable>
</dw:transform-message>
And here's w/ the transformer and set-variable
component:
<json:json-to-object-transformer returnClass="java.util.List" mimeType="application/java" doc:name="JSON to Object"/>
<set-variable variableName="alertIdPayload" value="#[payload[0].Name]" doc:name="Variable"/>
Hope this helps.
Upvotes: 0