Announcements
This site is in read only until July 22 as we migrate to a new platform; refer to this community post for more details.
Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

AM Policy Template JSON path function returns empty

I am using an AM Policy in Apigee Edge and I want to retrieve the root element "error" from the target response.content. In the current JSON path configuration the "flow.error.context" returns empty. However if i retrieve the whole content using Ref, the whole response is returned as expected. Does anyone know how to properly extract selected elements using AM Policy and JSON path?

<AssignVariable>
<Name>flow.error.context</Name>
<Ref>response.content</Ref>
</AssignVariable>

 

AM Policy:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AM-Error-404">
<DisplayName>AM-Error-404</DisplayName>
<AssignVariable>
<Name>flow.error.context</Name>
<Template>{jsonPath($.error,response.content)}</Template>
</AssignVariable>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</AssignMessage>

response.content:

{
        "error": {
            "detail": "",
            "type": "",
            "status": 404
        }
}
Solved Solved
0 5 524
2 ACCEPTED SOLUTIONS

You don't show your payload. See attached for a working example.  Follow the readme. Maybe this will help you. 

 

View solution in original post

Hey Dino,
Thanks for the example provided. I have provided my payload in the beginning of the topic. Here it is again:

response.content:
{
"error": {
"detail": "",
"type": "",
"status": 404
}
}

 

It looks like the JSON path function could only return literals, but not JSON fragments. In my case I successfully extracted "$error.status", but I wasn't able to extract "$.error". It seemed like the function couldn't handle and return JSON fragment as a string. The documentation mentions that the JSON path function returns a single element or an array, it is not thoroughly defined that only leaf elements are supported. Ref: https://docs.apigee.com/api-platform/reference/message-template-intro#json-path-function
It was my expectation that the JSON path function in AM policy behaves in the same way as the JSON path used to extract variable in an EV policy, which returns JSON fragments. In that case I would have to configure EV policy instead to retrieve the "$.error" object.

View solution in original post

5 REPLIES 5

 

Yes, sure. Try assigning the query to a variable and referencing THAT. So it would look like this: 

<AssignMessage name="AM-Error-404">
  <AssignVariable>
    <Name>query1</Name>
    <Value>$.error</Value>
  </AssignVariable>
  <AssignVariable>
    <Name>flow.error.context</Name>
    <!-- refer to the query indirectly, via a previously-set variable -->
    <Template>{jsonPath(query1,response.content)}</Template>
  </AssignVariable>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</AssignMessage>

Thank you for you replay. I had seen this solution and I did try it out and it still did not work. I tried using JSON path via EV policy which worked just fine, but I don't want to have additional EV policy if I could configure it all within AM policy. Is this an Apigee issue and are there any other workarounds ?
here is the EV policy which works fine:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="EV-SetTargetErrorContent-Response">
<DisplayName>EV-SetTargetErrorContent-Response</DisplayName>
<JSONPayload>
<Variable name="rich_context">
<JSONPath>$.error</JSONPath>
</Variable>
</JSONPayload>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<Source clearPayload="false">response</Source>
<VariablePrefix>flow.error</VariablePrefix>
</ExtractVariables>

You don't show your payload. See attached for a working example.  Follow the readme. Maybe this will help you. 

 

Hey Dino,
Thanks for the example provided. I have provided my payload in the beginning of the topic. Here it is again:

response.content:
{
"error": {
"detail": "",
"type": "",
"status": 404
}
}

 

It looks like the JSON path function could only return literals, but not JSON fragments. In my case I successfully extracted "$error.status", but I wasn't able to extract "$.error". It seemed like the function couldn't handle and return JSON fragment as a string. The documentation mentions that the JSON path function returns a single element or an array, it is not thoroughly defined that only leaf elements are supported. Ref: https://docs.apigee.com/api-platform/reference/message-template-intro#json-path-function
It was my expectation that the JSON path function in AM policy behaves in the same way as the JSON path used to extract variable in an EV policy, which returns JSON fragments. In that case I would have to configure EV policy instead to retrieve the "$.error" object.

Ahhh, I understand.  Good catch.  You are correct that the documentation of the jsonpath function does not clearly specify that. Sorry for the confusion. I'm glad you got it working anyway.