APIM Liquid template - context.Variables access content as JSON

GFPhillips 1 Reputation point

Hi All,

this is roughly the policy i have in place

        <send-request mode="new" response-variable-name="response">
            <!-- Calling external service and getting JSON response back-->
            <when condition="@(((IResponse)context.Variables["response"]).StatusCode == 200)">
                <set-variable name="myvariable" value="@(((IResponse)context.Variables["response"]).Body.As<string>())" />
                <!-- Do some other stuff-->
            <base />
        <backend />
            <set-header name="Content-Type" exists-action="override">
            <set-body template="liquid">
                <!-- PROBLEM: How do I access the variable as a json if i cant parse it on my template -->
            <base />
            <base />

How can i access context.Variables["myvariable"] as json on my liquid template? - This is currently a JSON string


Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
1,241 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Pramod Valavala 15,136 Reputation points Microsoft Employee

    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



    1 person found this answer helpful.
    0 comments No comments

  2. Oscar Garcia @ozkary 1 Reputation point MVP

    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>())" />
    0 comments No comments