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.
<br>{ <br> "messages": {<br> "staging": [ <br> { <br> "author" : "tim_bot",<br> "category": "information",<br> "message" : "Building..."<br> },<br> { <br> "author" : "tim_bot",<br> "category": "information",<br> "message" : "Testing..."<br> },<br> { <br> "author" : "tim_bot",<br> "category": "information",<br> "message" : "Completed!"<br> }<br> ],<br> "production": [<br> { <br> "author" : "jill_bot",<br> "category": "information",<br> "message" : "Building..."<br> },<br> { <br> "author" : "jill_bot",<br> "category": "error",<br> "message" : "Build Failed!"<br> }<br> ]<br> }<br>}<br> |
<br><?xml version="1.0" encoding="UTF-8" ?><br><messages><br> <staging><br> <author>tim_bot</author><br> <category>information</category><br> <message>Building...</message><br> </staging><br> <staging><br> <author>tim_bot</author><br> <category>information</category><br> <message>Testing...</message><br> </staging><br> <staging><br> <author>tim_bot</author><br> <category>information</category><br> <message>Completed!</message><br> </staging><br> <production><br> <author>jill_bot</author><br> <category>information</category><br> <message>Building...</message><br> </production><br> <production><br> <author>jill_bot</author><br> <category>error</category><br> <message>Build Failed!</message><br> </production><br></messages><br> |
---|
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. |