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

Javascript JSON object not being cached

Not applicable

When you try to cache a JSON object (or any object) there is an issue where it can only be retrieved intermittently.

This happens when you try to store a JSON object in the cache, e.g.

var myJson = {"Name": "value"};

context.setVariable("VAR_NAME", myJson);

When you then try to cache VAR_NAME it will store it in the MP but Cassandra will not accept it as an object. This means that when you try to lookup the cache it will work on the original MP that tried to store it but not on any others, i.e. sometimes you'll get the value, sometimes you'll get null.

The way to fix this is to store the JSON object as a string by either:

var myJson = {"Name": "value"};

context.setVariable("VAR_NAME", JSON.stringify(myJson));

or simply

var myJson = '{"Name": "value"}';

One advantage of this over, say wrapping your object in single quotes, is that you can reference the lookup variable in javascript as follows:

context.getVariable("lookup_var")
1 3 782
3 REPLIES 3
Top Solution Authors