The jsonHolder object

The jsonHolder object provides access to the request/response's JSON payload.

Properties

  • context.jsonHolder.topNode : returns the JSON payload as a JavaScript object. This object can be changed using JavaScript, and these changes will be reflected in the forwarded request/response.

  • context.jsonHolder.json : returns the JSON payload as a string, if you prefer to parse or process the JSON payload yourself. This variable's value can be set to a JSON string.

  • context.jsonHolder.jsonPath : returns a jsonPath object, which can be used to execute JSON Path expressions for querying and modifying the payload.

Methods

object getByJsonPointer(string ptr) : retrieves a part of the JSON payload using a JSON Pointer expression. The returned object (if any) is a regular JavaScript object and can be modified using JavaScript code. The main advantage of JSON pointers over simply accessing the objects directly is that you can include spaces in name.

Examples

In the following examples, we will assume that the payload is:

{

"store": {

"books": [

{

"category": "reference",

"author": "Nigel Rees",

"title": "Sayings of the Century",

"price": 8.95,

"onSale": false

},

{

"category": "fiction",

"author": "Evelyn Waugh",

"title": "Sword of Honour",

"price": 12.99,

"onSale": true

}

],

"bicycle": {

"color": "red",

"price": 19.95,

"onSale": false

}

},

"expensive": 10

}


Simple modification to a payload:

let p = context.jsonHolder.topNode;

p.expensive = p.expensive + 2;

p.store.books[1].price = 9.99;


Retrieving a node using a JSON Pointer:

let bicycle = context.jsonHolder.getByJsonPointer("/store/bicycle");

bicycle.color = "reddish";

This is equivalent to:

context.jsonHolder.topNode.store.bicycle.color = "reddish";

If you need to access properties deep in the document, and some of the names include spaces or other special characters, JSON pointers may be more readable:

let price = context.jsonHolder.topNode['sub node']['sub sub node'].price;

let price = context.jsonHolder.getByJsonPointer("/sub node/sub sub node/price");


Retrieve attribute values using JSON path:

let bookPrices = context.jsonHolder.jsonPath.read("$.store.books[*].price);

// Returns [8.95, 12.99]

See the jsonPath object documentation for more examples.