JSON Add Node

Overview

You can use this filter to add a node to a JavaScript Object Notation (JSON) document. The new node is inserted into the location specified by a JSON Path expression. JSON Path is a query language that enables you to select nodes in a JSON document.

For more details on JSON Path, see http://code.google.com/p/jsonpath.

Configuration

You can configure the following settings:

Name:

Enter a suitable name that reflects the role of this filter in the policy.

JSON Path Expression:

Enter the JSON Path expression used to add the node to the JSON document (for example, $.store). The Policy Studio prompts you if you enter an unsupported JSON Path expression.

[Note] Note

If this expression returns more than one node, the first node is used. If the expression returns no nodes, the filter returns false.

Node Source:

Enter the JSON node to be inserted into the message. For example, the following node source represents a new car:

{"make":"Ford",
	"airbags":true,
	"doors":4
	"price":1111.00
}

Select one of the following options for the source of the new node:

  • Add as a new item to an array:

    If you select this option, the new JSON node is added as an item in an array.

  • Add as a new item with field name:

    If you select this option, the new JSON node is added as a field specified in Field Name text box (for example, car).

  • Insert previously removed nodes:

    You can configure a JSON Remove Node filter to remove JSON nodes from the message and store them in the deleted.json.node.list message attribute. You can then use the JSON Add Node filter to reinsert these nodes in a different location in the message, effectively moving the deleted nodes in the message. When selecting this option, you must also select Save deleted nodes to be reinserted to new location in the Remove JSON Node filter, which runs before the Add JSON Node filter in the policy. For more details, see the topic on JSON Remove Node.

What to do with any existing siblings in the container:

Select one of the following options to determine where the new node is placed relative to the node(s) returned by the JSON Path expression:

  • Append:

    The new node is appended as a child node of the node returned by the JSON Path expression. If there are already child nodes of the node returned by the JSON expression, the new node is added as the last child node.

  • Replace:

    The node pointed to by the JSON expression is completely replaced by the new node.

Examples

The following are some examples of using the JSON Add Node filter to add and replace JSON nodes.

Adding a Node

The following example shows the settings required to add a car node to the store:

Adding a JSON Node

The following example shows the corresponding request and response message in Oracle API Gateway Explorer:

Added JSON Node

Adding an Item to an Array

The following example shows the settings required to add a book to an array:

Added JSON Node

The following example shows the corresponding request and response message in Oracle API Gateway Explorer:

Added JSON Node

Adding a Field Replacing Others

The following example shows the settings required to add a field to the bicycle, removing any other fields that may exist:

Added JSON Node

The following example shows the corresponding request and response message in Oracle API Gateway Explorer:

Added JSON Node