Azure API policy JSON conversion while returning the response from cache

I am using below policy code and storing the data in the cache. While storing as string, it is working fine but requirement is to store in JSON format and its failing and throwing error during conversion .

<policies>
    <inbound>
        <base />
        <set-variable name="cacheKey" value="@(context.Request.MatchedParameters["flightno"])" />
        <cache-lookup-value key="@((string)context.Variables["cacheKey"])" variable-name="cachedResponseValue" />
        <set-variable name="FinalResponse" value="@((JObject)context.Variables["cachedResponseValue"])" />
        <choose>
            <when condition="@(context.Variables.ContainsKey("cachedResponseValue"))">
                <!-- If found in cache then get it from the cache and retun -->
                <return-response>
                    <set-body>@((JObject)context.Variables["FinalResponse"])</set-body>
                </return-response>
            </when>
        </choose>    
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
        <set-variable name="responseValue" value="@(context.Response.Body.As<JObject>(preserveContent: true))" />
        <!-- Store result in cache -->
        <cache-store-value key="@("" + context.Variables["cacheKey"])" value="@((JObject)context.Variables["responseValue"])" duration="30" />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

I'm able to store the payload as json in the cache, while retrieving,I want to return json and trying to parse it. Error am getting while parsing the response in tag for below syntax as

<set-body>@((JObject)context.Variables["FinalResponse"])</set-body>


Error in element 'set-body' on line 22, column 22: Expression return type 'Newtonsoft.Json.Linq.JObject' is not allowed.

It is working fine, if response data type kept as string like

 <set-body>@((string)context.Variables["FinalResponse"])</set-body>

Do you know how to convert the response body to json?

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum