Extract Variables Policy with JSON payload type string but cannot manipulate in node

Not applicable

The request has a payload like:

{"books": ["a", "b", "c"]} and I store it into the variable "myBooks" using an Extract Variables Policy

In node.js I use apigee-access to get the extracted variable:

var myNodeBooks = apigee.getVariable(req, 'myBooks');

console.log(myNodeBooks) gives what you'd expect:

["a","b","c"]

However, I can't operate on the variable at all.

myNodeBooks[0] breaks at runtime. So does myNodeBooks.split(","). If I send a request to BaaS with the variable, BaaS just ignores it.

My suspicion is it's a problem with the data type but I'm hoping someone with my experience could clarify.

Thanks.

0 4 1,304
4 REPLIES 4

Hi, @Edward,

I believe your payload {"books": ["a", "b", "c"]} is in string format.

If you want to use it in node.js it should be json object. So try JSON.parse() on it and then you can further apply javascript functions on with it.

If it's in string format, shouldn't I be able to call split() on it and convert it to an array? Also JSON.parse throws an unhelpful error.

What error it gives on JSON.parse()?

Surprisingly, it worked with JSON.parse()...but if the extracted variable is indeed a string, why can't I split it like I can with any other string?

I could've sworn I tried that. Perhaps it was because I didn't specify the type of the extracted variable in the Extract Variables Policy (although I would expect it to default to string anyway).

The error that haunted me was:

Script execution failed: Access to Java class \"com.apigee.jsonparser.JSONArray\" is prohibited.