Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,118 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi All,
this is roughly the policy i have in place
<policies>
<inbound>
<send-request mode="new" response-variable-name="response">
<!-- Calling external service and getting JSON response back-->
</send-request>
<choose>
<when condition="@(((IResponse)context.Variables["response"]).StatusCode == 200)">
<set-variable name="myvariable" value="@(((IResponse)context.Variables["response"]).Body.As<string>())" />
</when>
</choose>
<otherwise>
<!-- Do some other stuff-->
</otherwise>
<base />
</inbound>
<backend />
<outbound>
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body template="liquid">
<!-- PROBLEM: How do I access the variable as a json if i cant parse it on my template -->
{{context.Variables["myvariable"]}}
</set-body>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
How can i access context.Variables["myvariable"] as json on my liquid template? - This is currently a JSON string
Thanks
There doesn't seem to be any filter present in liquid or its C# implementation, DotLiquid that allows for parsing JSON strings. But here is a workaround that I can confirm works
An approach is to save the variable as an object instead. This should persist the JSON document
<set-variable name="myvariable" value="@(((IResponse)context.Variables["response"]).Body.As<JObject>())" />