JsonCBR
JsonCBR (Json Content Based Routing) is used to route the incoming messages onto different destinations based on the content of the messages. The component creates a port for each of the JsonPath expressions specified and the messages satisfying the particular JsonPath expression is sent onto the respective port. In addition to these ports, an output port OUT_FALSE is created and messages whose content does not satisfy any of the JsonPath expressions will be sent out of this port. If more than one JsonPath expression is true, the message is sent on all the ports for which the JsonPath expression evaluates to true.
A Json expression is a combination of a JsonPath, operator, and a value (if needed).
Configuration and Testing
Routing Configuration
The JsonCBR component can be configured using its Custom Property Sheet (CPS) wizard.
Figure 1: JsonPath(s) to be evaluated on the input messages
In the JsonCBR Configuration pane, the Add button helps to add configuration properties, the Remove button helps to remove the selected configuration property and Remove All helps to clear the entire configuration.
JsonPath
JsonPaths, based on which the routing is done, can be configured in JsonCBR Configuration Page. A valid JsonPath expression can be typed directly into the table.
For each configured JsonPath expression, an output port is created with the name provided in Port Name column, messages satisfying the given Json Objects condition are routed onto this port. Port names ON_EXCEPTION and OUT_FALSE are reserved and cannot be set in Port Name column.
- Json values which are passed in input Json object should only be any of these types: Long, Integer, Double, String, Boolean, BigInteger, BigDecimal, or another JsonPath.
Comparison of the JSONPath syntax elements with its XPath counterparts | ||
XPath | JSONPath | Description |
/ | $ | the root object/element |
.. | n/a | parent operator |
// | .. | recursive descent. JSONPath borrows this syntax from E4X. |
* | * | wildcard. All objects/elements regardless of their names. |
@ | n/a | attribute access. JSON structures don't have attributes. |
[] | [] | subscript operator. XPath uses it to iterate over element collections and for predicates. In Javascript and JSON, it is the native array operator. |
| | [,] | Union operator in XPath results in a combination of node sets. JSONPath allows alternate names or array indices as a set. |
n/a | [start:end:step] | array slice operator borrowed from ES4. |
[] | ?() | applies a filter (script) expression. |
n/a | () | script expression, using the underlying script engine. |
() | n/a | grouping in XPath |
For more information on the XPath for Json, please refer to the article at http://goessner.net/articles/JsonPath..
Operators
This field includes boolean operators like ==, !=, <, >, <=, >=, EXISTS, NONEXISTENT.
- The EXISTS operator returns true if the given JsonPath element exists in the input Json.
- While NONEXISTENT operator returns true when the given JsonPath element is nonexistent in the input Json.
Value
This field can be any of these types: Long, Integer, Double, String, Boolean, BigInteger, BigDecimal, and JsonPath. If a "$" precedes any value, then it is treated as a JsonPath. In this case, the values of the two JsonPaths are compared for evaluating the expression.
Error Handling Configuration
Allows configuring actions to be taken when an exception occurs during the execution.
Figure 2: Error Handling configuration properties in the next panel
Following are the configuration options:
- JMS Error
- Response Generation Error
- Request Processing Error
- Invalid Request Error
For descriptions, refer to the Error Handling section in the Common Configurations page.
Functional Demonstration
Scenario
JsonCBR is configured to filter the messages on the basis of book price in the input message. It is sent to one of the output ports depending on the JsonPath specified in the JsonCBR CPS.
Configure the JsonCBR as described in (11.0) JsonCBR#Configuration and Testing section and use feeder and display component to send sample input and check the response respectively.
Figure 3: Demonstrating the scenario with sample input and output
Sample Input
{ "store": {
"book": [
{ "category": "reference",
"author": "Nigel Rees",
"title": "My Radio Times",
"price": 8.95
},
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{ "category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{ "category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
Sample Output
JsonOutput5: As $.store.book[0].price is 8.95 which is less than 18.4, the following output is displayed.
JsonOutput5
{ "store": {
"book": [
{ "category": "reference",
"author": "Nigel Rees",
"title": "My Radio Times",
"price": 8.95
},
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{ "category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{ "category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
JsonOutput4: As $.store.book[1].title is "Sword of Honour", which is not equal to "abc", the following output is displayed.
JsonOutput4
{ "store": {
"book": [
{ "category": "reference",
"author": "Nigel Rees",
"title": "My Radio Times",
"price": 8.95
},
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{ "category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{ "category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
JsonOutput3: As $.store.book[2].category is "fiction", which is equal to $.store.book[1].category, the following output is displayed.
JsonOutput3
{ "store": {
"book": [
{ "category": "reference",
"author": "Nigel Rees",
"title": "My Radio Times",
"price": 8.95
},
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{ "category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{ "category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
JsonOutput2: As $.store.book[0].isbn does not exist, no output is displayed.
JsonOutput1: As some Json expressions were evaluated as 'true', the OUT_FALSE gets no output, hence no output is displayed.