JSONPath: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
Line 62: Line 62:
====<tt>[-''n'':]</tt>====
====<tt>[-''n'':]</tt>====
The expression selects the last ''n'' elements of the array. It returns <font color=darkgray>a space-separated list</font>. If more elements than available are requested, the result is tool-dependent. For example, [[Kubectl_get_JSONPath_Support#Overview|kubectl JSONPath support]] raises an error.
The expression selects the last ''n'' elements of the array. It returns <font color=darkgray>a space-separated list</font>. If more elements than available are requested, the result is tool-dependent. For example, [[Kubectl_get_JSONPath_Support#Overview|kubectl JSONPath support]] raises an error.
====Array Filter Expressions====
<syntaxhighlight lang='bash'>
[?(expression)]
</syntaxhighlight>
===* (Wildcard)===
===* (Wildcard)===
Wildcard applies to both Object and Array elements and selects all fields/array elements, regardless of their names or indexes.
Wildcard applies to both Object and Array elements and selects all fields/array elements, regardless of their names or indexes.
Line 88: Line 83:
2021-03-03T18:40:00Z 2021-03-05T17:29:23Z 2021-03-05T17:29:23Z 2021-03-03T18:40:00Z
2021-03-03T18:40:00Z 2021-03-05T17:29:23Z 2021-03-05T17:29:23Z 2021-03-03T18:40:00Z
</syntaxhighlight>
</syntaxhighlight>
===Array Filter Expressions===
<syntaxhighlight lang='bash'>
[?(expression)]
</syntaxhighlight>


===Script Expressions===
===Script Expressions===

Revision as of 01:43, 16 March 2021

External

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

The expressions, including fields and values, are case-sensitive. Unlike XPath, JSONPath does not have operations for accessing parent or sibling nodes from a given nodes. 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: the dot notation .field (or its variant ['field ']) and index-based selection. These two different forms are applied to JSON Objects and Arrays, respectively, given the fact that JSON has only these two types of collections.

Object Fields Selection

.field

This is called "the dot notation". The expression selects the specified field in the parent JSON Object.

.metadata.resourceVersion

['field']

This is an equivalent variant of the dot notation that can used to select the specified field of a parent Object. It requires quotes around the field name and it is useful when the field name contains special characters such as spaces, or they begin with a character other than A..Za..z_.

['metadata']['resourceVersion']

Note that some tools, such as kubectl JSONPath support, do not support this notation.

Array Element Selection

Array elements are selected using 0-based indexes.

[0-based-index]

The expression selects the element whose index is specified between square brackets from a JSON Array.

[index1, index2, ...]

The expression selects the elements with the specified indexes from a JSON Array and returns a space-separated list. If the element with the specified index does not exist, the result is tool-dependent. For example, kubectl JSONPath support raises an error.

[start:end], [start:]

The expression selects the elements from the start index and up to, but not including, the end index. If end is omitted, it selects all elements from start until the end of the array. It returns a space-separated list. If the element with the specified index does not exist, the result is tool-dependent. For example, kubectl JSONPath support raises an error.

[:n]

The expression selects the first n elements of the array. It returns a space-separated list. If more elements than available are requested, the result is tool-dependent. For example, kubectl JSONPath support raises an error.

[-n:]

The expression selects the last n elements of the array. It returns a space-separated list. If more elements than available are requested, the result is tool-dependent. For example, kubectl JSONPath support raises an error.

* (Wildcard)

Wildcard applies to both Object and Array elements and selects all fields/array elements, regardless of their names or indexes.

To apply it to an Object element:

.metadata.*

To apply it to an Array:

.status.conditions[*]

Note that dot notation can be applied to the result of the wildcard expression. If the expression is applied to an array, and there are more than one element in an array, a space separated list will be returned:

.status.conditions[*].lastTransitionTime

returns:

2021-03-03T18:40:00Z 2021-03-05T17:29:23Z 2021-03-05T17:29:23Z 2021-03-03T18:40:00Z

Array Filter Expressions

[?(expression)]


Script Expressions

[(expression)]

JSONPath Template