0
votes

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?

2

2 Answers

1
votes

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"/>
0
votes

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.