Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Get and parse String from cache to JSON

Not applicable

Hi everybody again. I store the String in JSON format via populate cache. Here is the format of the string

{
        "issued": "1461251312541",
        "ttl": "3600",
        "owner": "test1"
}

My LookUpCachePolicy

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<LookupCache async="false" continueOnError="false" enabled="true" name="GetDetailsFromCache">
    <DisplayName>GetDetailsFromCache</DisplayName>
    <Properties/>
    <CacheKey>
        <KeyFragment ref="request.header.usertoken"/>
    </CacheKey>
    <CacheResource>SessionCache</CacheResource>
    <Scope>Global</Scope>
    <AssignTo>DetailsFromCache</AssignTo>
</LookupCache>

Then i try to implement ExtractVariable policy

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="ExtractUsernameFromJSON">
    <DisplayName>ExtractUsernameFromJSON</DisplayName>
    <Properties/>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <JSONPayload>
        <Variable name="name">
            <JSONPath>$.owner</JSONPath>
        </Variable>
    </JSONPayload>
    <Source clearPayload="false">DetailsFromCache</Source>
</ExtractVariables>

But i get 500 error with error message

{"fault":{"detail":{"errorcode":"Internal Server Error"},"faultstring":"com.apigee.jsonparser.LinkedJSONObject cannot be cast to java.lang.String"}}

What am I doing wrong? Thank you.

Solved Solved
1 3 951
1 ACCEPTED SOLUTION

Not applicable

This is an easy mistake to make. When using extract variables your source must be a request or response obj. In a response cache this would work fine because your working with a response but with a lookup, populate cache you are working with raw variables. To use the extractvariables policy against a variable you have to use the AssignMessage policy and put the value of the variable in the payload with a contentType of application/json

e.g.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="setVariable">
    <DisplayName>setVariable</DisplayName>
    <Properties/>
    <Set>
         <Payload contnetType="application/json" variablePrefix="#" variableSuffix="%">#DetailsFromCache%</Payload>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="true" transport="http" type="response">someVar</AssignTo>
</AssignMessage>

View solution in original post

3 REPLIES 3