jsonPath not defined in Javascript policy?

Hi,

we are trying to extract elements from a json payload by using jsonPath in Javascript policy, but we are getting an error

Javascript runtime error: "ReferenceError: "jsonPath" is not defined. (JavaScript_1_js#5). at line 5

Is jsonPath not defined in Javascript policy??

We cannot use a extract variable policy here because we need to replace some elements from the json input after modifying them based in certain conditions.

Thanks in advance.

Solved Solved
0 5 2,238
1 ACCEPTED SOLUTION

Check this out: https://code.google.com/p/jsonpath/

You can include jsonpath-0.8.0.js in your bundle and set it as an "include" to your Javascript policy:

  1. Download jsonpath-0.8.0.js and put it under apiproxy/resources/jsc
  2. Include it in your Javascript policy:
<Javascript name="JavaScript-1">
    <ResourceURL>jsc://JavaScript-1.js</ResourceURL>
    <IncludeURL>jsc://jsonpath-0.8.0.js</IncludeURL>
</Javascript

Now you can use jsonPath function in your js code:

var json = { "MovieDatabase": {
	"movie": [ 
	{ "name":"The Change-Up",
		"genre": "comedy",
		"director": "David Dobkin",
		"Facebook_like": 252
	},
	{ "name":"Rise of the Planet of the Apes",
		"genre": "SciFi",
		"director": "Rupert Wyatt",
		"Facebook_like": 472
	},
	{ "name":"30 Minutes or Less",
		"genre": "adventure",
		"director": "Ruben Fleischer",
		"Facebook_like": 114
	},
	{ "name":"Final Destination 5",
		"genre": "Horror",
		"director": "Steven Quale",
		"Facebook_like": 241
	}
	]
}
};


var a = jsonPath(json, "$.MovieDatabase.movie[*].director");


a.forEach(function(director) {
  context.setVariable(director, director);
});

View solution in original post

5 REPLIES 5

Unfortunately I don't have an answer for you...but I see that error as well. A workaround would be to use Extract variable policy to extract the desired value using JSONPath and assign it to a variable. In your JS use that variable for further processing. It should add very little overhead on the overall proxy performance.

Check this out: https://code.google.com/p/jsonpath/

You can include jsonpath-0.8.0.js in your bundle and set it as an "include" to your Javascript policy:

  1. Download jsonpath-0.8.0.js and put it under apiproxy/resources/jsc
  2. Include it in your Javascript policy:
<Javascript name="JavaScript-1">
    <ResourceURL>jsc://JavaScript-1.js</ResourceURL>
    <IncludeURL>jsc://jsonpath-0.8.0.js</IncludeURL>
</Javascript

Now you can use jsonPath function in your js code:

var json = { "MovieDatabase": {
	"movie": [ 
	{ "name":"The Change-Up",
		"genre": "comedy",
		"director": "David Dobkin",
		"Facebook_like": 252
	},
	{ "name":"Rise of the Planet of the Apes",
		"genre": "SciFi",
		"director": "Rupert Wyatt",
		"Facebook_like": 472
	},
	{ "name":"30 Minutes or Less",
		"genre": "adventure",
		"director": "Ruben Fleischer",
		"Facebook_like": 114
	},
	{ "name":"Final Destination 5",
		"genre": "Horror",
		"director": "Steven Quale",
		"Facebook_like": 241
	}
	]
}
};


var a = jsonPath(json, "$.MovieDatabase.movie[*].director");


a.forEach(function(director) {
  context.setVariable(director, director);
});

Great answer, Ozan! very clear.

Not applicable

Hi @Prashanthi,

If you plan to use the jsonPath or other libraries across multiple proxies, you may want to consider storing the relevant resource(s) at the environment or organization level.

Here is documentation that explains how to store and reference resource files at the proxy, environment and organization levels.

Nice tip @pparekh