Search code examples
javascriptarraysjsonapigeejsonpath

jsonPath not working in apigee javascript policy


We have a json input from which we have to decode each element in the input and replace it back before going to the backend. we have used a Extract Variables policy to extract the elements, and in a Javascript policy we decode the elements by using a for loop. Now we have to replace the extracted elements with the decoded values. We tried to use jsonPath and replace the elements but getting a error called

Execution of JavaScript-1 failed with error: Javascript runtime error: "ReferenceError: "jsonPath" is not defined. (JavaScript_1_js#5). at line 5

The code we used for jsonpath is

var input = new String(context.getVariable("request.content"));
context.setVariable("input",input);

var path = jsonPath(input, "$..soldto").toJSONString();
context.setVariable("path",path);

Can we not use jsonPath in Apigee Javascript policy?


Solution

  • By default if JSONPath is not present - you can download and add it to your proxy bundle. You reference it using includeURL in the JS policy. This make it available to your JS code for use.

    http://apigee.com/docs/api-services/reference/javascript-policy#includeurl

    Also take a look at this same on how to use a custom JS lib which is not included.

    https://github.com/apigee/api-platform-samples/tree/master/sample-proxies/base64encoder/apiproxy

    The sample uses crypto JS.