JSONPath: Difference between revisions
No edit summary |
|||
Line 12: | Line 12: | ||
JSONPath is a query language for JSON similar to [[XPath]] for [[XML]]. Some documentation describes it as a Java DSL for reading JSON documents, probably to address those cases where the implementation is made in Java. | JSONPath is a query language for JSON similar to [[XPath]] for [[XML]]. Some documentation describes it as a Java DSL for reading JSON documents, probably to address those cases where the implementation is made in Java. | ||
=JSONPath Expression= | |||
A JSONPath expression specifies a [[JSON_Concepts#Path|path]] to an [[JSON_Concepts#Element|element]] or a set of elements that are part of a [[JSON_Concepts#JSON_Document|JSON document]]. | A JSONPath expression specifies a [[JSON_Concepts#Path|path]] to an [[JSON_Concepts#Element|element]] or a set of elements that are part of a [[JSON_Concepts#JSON_Document|JSON document]]. | ||
<syntaxhighlight lang='json'> | <syntaxhighlight lang='json'> | ||
Line 20: | Line 19: | ||
An expression can be assembled using a series of syntax elements presented below. | An expression can be assembled using a series of syntax elements presented below. | ||
==<tt>$</tt>== | |||
$ represents the [[JSON_Concepts#Top-Level_Element|top-level element]] (or root) of the JSON document. The "$" is optional, the following are equivalent: | $ represents the [[JSON_Concepts#Top-Level_Element|top-level element]] (or root) of the JSON document. The "$" is optional, the following are equivalent: | ||
Line 29: | Line 28: | ||
Some JSONPath documentation mentions that even the leading dot can be omitted, but that may break some tools, as it is the case with [[Kubectl_get_JSONPath_Support#Overview|kubectl JSONPath support]]. | Some JSONPath documentation mentions that even the leading dot can be omitted, but that may break some tools, as it is the case with [[Kubectl_get_JSONPath_Support#Overview|kubectl JSONPath support]]. | ||
==Element Selection== | |||
There are two ways to select a specific child element relative to the parent element: [[#.property|.''field'']] and [[#.5B0-based-index.5D|[''0-based-index'']]]. These two different syntaxes are applied to JSON [[JSON_Concepts#Objects|Objects]] and [[JSON_Concepts#Arrays|Arrays]], given the fact that JSON has only these two types of collections. | There are two ways to select a specific child element relative to the parent element: [[#.property|.''field'']] and [[#.5B0-based-index.5D|[''0-based-index'']]]. These two different syntaxes are applied to JSON [[JSON_Concepts#Objects|Objects]] and [[JSON_Concepts#Arrays|Arrays]], given the fact that JSON has only these two types of collections. | ||
Line 41: | Line 40: | ||
====<tt>[''0-based-index'']</tt>==== | ====<tt>[''0-based-index'']</tt>==== | ||
=JSONPath Template= |
Revision as of 00:25, 16 March 2021
External
- https://goessner.net/articles/JsonPath/
- https://github.com/json-path/JsonPath
- https://support.smartbear.com/alertsite/docs/monitors/api/endpoint/jsonpath.html
- https://github.com/jayway/JsonPath
Internal
Overview
JSONPath is a query language for JSON similar to XPath for XML. Some documentation describes it as a Java DSL for reading JSON documents, probably to address those cases where the implementation is made in Java.
JSONPath Expression
A JSONPath expression specifies a path to an element or a set of elements that are part of a JSON document.
.spec.containers[0].name
An expression can be assembled using a series of syntax elements presented below.
$
$ represents the top-level element (or root) of the JSON document. The "$" is optional, the following are equivalent:
$.metadata
.metadata
Some JSONPath documentation mentions that even the leading dot can be omitted, but that may break some tools, as it is the case with kubectl JSONPath support.
Element Selection
There are two ways to select a specific child element relative to the parent element: .field and [0-based-index]. These two different syntaxes are applied to JSON Objects and Arrays, given the fact that JSON has only these two types of collections.
.field
This is called "the dot notation". The expression selects the specified field in the parent JSON Object.
.metadata.resourceVersion