These query languages can be used to extract values from JSON or XML. Each has its own syntax allowing you to select a node to extract a value from. The table below shows some of the syntax allowed by each language.
XPath | JSONPath | Description |
/ | $ | The base node in the object. |
. | @ | The current object in the node path. |
/ | . or [] | Accessing a child node, or specific child node |
.. | n/a | Accessing the parent of the current node. |
// | .. | Recursive child decent. |
* | * | Wildcard operator to access any node. |
@ | n/a | Accessing an attribute. |
[] | [] | Accessing an element within a list of nodes. |
| | [,] | Union of two sets of nodes. |
n/a | [start:end:step] | Accessing elements as a slice of a set of elements. |
[] | ?() | Accessing nodes with a filter applied. |
Here is some example text in JSON and XML.
{ "messages": { "staging": [ { "author" : "tim_bot", "category": "information", "message" : "Building..." }, { "author" : "tim_bot", "category": "information", "message" : "Testing..." }, { "author" : "tim_bot", "category": "information", "message" : "Completed!" } ], "production": [ { "author" : "jill_bot", "category": "information", "message" : "Building..." }, { "author" : "jill_bot", "category": "error", "message" : "Build Failed!" } ] } } | <?xml version="1.0" encoding="UTF-8" ?> <messages> <staging> <author>tim_bot</author> <category>information</category> <message>Building...</message> </staging> <staging> <author>tim_bot</author> <category>information</category> <message>Testing...</message> </staging> <staging> <author>tim_bot</author> <category>information</category> <message>Completed!</message> </staging> <production> <author>jill_bot</author> <category>information</category> <message>Building...</message> </production> <production> <author>jill_bot</author> <category>error</category> <message>Build Failed!</message> </production> </messages> |
---|
The following are XPath and JSONPath examples that could be used to extract information from the example text.
XPath | JSONPath | Result |
/messages/staging/author | $.messages.staging[*].author | The authors of all messages sent to staging |
//author | $..author | All the authors of all messages |
/messages/* | $.messages.* | All channels messages were sent to. Staging and production. |
/messages//category | $.messages..category | The categories of all messages. |
//staging[3] | $..staging[2] | The third message sent to staging. |
//staging[last()] | $..staging[(@.length-1)] $..staging[-1:] | The last message sent to staging. |
//staging[position()<3] | $..staging[0,1] $..staging[:2] | The first two messages sent to staging. |
//staging[category="error"] | $..staging[?(@.category="error")] | All the messages sent to staging with an error category. This would return no values. |
//* | $..* | All nodes in the response. |