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 843
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

@Oleksandr Skoryi , What does trace show you ? Do you see variables getting populated ?

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>

Hi @srichardson,

Just wanted some clarification. Is it always behave like this?

As I have worked on similar scenario where I am populating a cache with JSON payload as:

<Source>myPayload</Source>, where myPayload is set as request.content using Assign Message policy.

and when I perform lookup cache for the same and store the cache data in a variable called "LookupCacheVar", and then use Extract Variable policy to extract each JSON field as below:

<Source clearPayload="false">LookupCacheVar</Source>
<JSONPayload>
<Variable name="VarStreetNumber" type = "string"> <JSONPath>$.someData.someAddress.streetNumber</JSONPath>
</Variable>
</JSONPayload>

I am able to do that.

I have to use Assign Message policy as mentioned by you when I have to perform XSLT transform on cache data after retrieving it using lookupcache as in XSLT transform the source has to be message type.

Please let me know if I am missing something here.