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

Invalid JSON path error in Extract Variable

Below is my EV policy code

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables continueOnError="false" enabled="true" name="EV-UserHREF">
<DisplayName>EV-UserHREF</DisplayName>
<Properties/>
<JSONPayload>
<Variable name="user_href">
<JSONPath>$.id</JSONPath>
</Variable>
</JSONPayload>
<Source>response</Source>
</ExtractVariables>

Below is the error I get. 

{
"fault": {
"faultstring": "Invalid JSON path $.id in policy EV-UserHREF.",
"detail": {
"errorcode": "steps.extractvariables.InvalidJSONPath"
}
}
}

I have passed Content-Type as application/json  while calling proxy from POSTMAN.

EV policy is in target endpoint preflow response.

I have also test json xpath in xpath finder and it gives me correct path but it is not working in EV.

Please suggest.

Solved Solved
0 4 1,785
1 ACCEPTED SOLUTION

Below polices has worked for me

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="AM-ContentType">
    <DisplayName>AM-ContentType</DisplayName>
    <Properties/>
    <Add>
        <Headers>
            <Header name="Content-Type">application/json</Header>
        </Headers>
    </Add>
    <AssignTo createNew="false" transport="http" type="response">varcomment</AssignTo>
</AssignMessage>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables continueOnError="false" enabled="true" name="EV-UserHREF">
    <DisplayName>EV-UserHREF</DisplayName>
    <Properties/>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <VariablePrefix>userurl</VariablePrefix>
    <JSONPayload>
        <Variable name="userhref">
            <JSONPath>$.[0]._links.self.href</JSONPath>
        </Variable>
    </JSONPayload>
    <Source>response</Source>
</ExtractVariables>

View solution in original post

4 REPLIES 4

I have passed Content-Type as application/json while calling proxy from POSTMAN.

Your EV policy is referring to the response message. So it doesn't matter what you sent IN to the proxy. It's trying to read the response, that Apigee has received from the target. Can you show the content that the proxy has received from the target?

Also - Have you attached the policy in the response flow? If you attach that policy in the request flow, I suppose it would result in a runtime error. In the request flow, the response object is empty (or maybe not defined). So if that's what you've got in your proxy, then I would expect to see "Invalid JSON path".

I have also test json xpath in xpath finder and it gives me correct path but it is not working in EV.

Things to check: What content did you use for the JSON? Is that the same content you're seeing in the response?  Is the response sent with Content-Type = application/json ?

I did set the Content-Type using AM as below and now the error is gone. But I still do not get the value in the variable 'userhref' which I used in EV.

Following are the policies now:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="Assign-Message-2">
    <DisplayName>Assign Message-2</DisplayName>
    <Properties/>
    <AssignVariable>
        <Name>varjson</Name>
        <Ref>response.content</Ref>
    </AssignVariable>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="response"/>
</AssignMessage>


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="AM-ContentType">
    <DisplayName>AM-ContentType</DisplayName>
    <Properties/>
    <Add>
        <Headers>
            <Header name="Content-Type">application/json</Header>
        </Headers>
    </Add>
    <AssignTo createNew="false" transport="http" type="response">varcomment</AssignTo>
</AssignMessage>



<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables continueOnError="false" enabled="true" name="EV-UserHREF">
    <DisplayName>EV-UserHREF</DisplayName>
    <Properties/>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <VariablePrefix>myprefix</VariablePrefix>
    <JSONPayload>
        <Variable name="userhref">
            <JSONPath>x[0].id</JSONPath>
        </Variable>
    </JSONPayload>
    <Source>response.content</Source>
</ExtractVariables>

I have attached these policies in the response preflow of target endpoint

You have a VariablePrefix element specified in the ExtractVariables policy. The value there is myprefix. That means the output variable, if the ExtractVaribles policy succeeds, will be named myprefix.userhref .

Below polices has worked for me

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="AM-ContentType">
    <DisplayName>AM-ContentType</DisplayName>
    <Properties/>
    <Add>
        <Headers>
            <Header name="Content-Type">application/json</Header>
        </Headers>
    </Add>
    <AssignTo createNew="false" transport="http" type="response">varcomment</AssignTo>
</AssignMessage>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables continueOnError="false" enabled="true" name="EV-UserHREF">
    <DisplayName>EV-UserHREF</DisplayName>
    <Properties/>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <VariablePrefix>userurl</VariablePrefix>
    <JSONPayload>
        <Variable name="userhref">
            <JSONPath>$.[0]._links.self.href</JSONPath>
        </Variable>
    </JSONPayload>
    <Source>response</Source>
</ExtractVariables>