JSONPath: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 30: Line 30:
==Element Selection==
==Element Selection==


There are two ways to select a specific child element relative to the parent element: the dot notation [[#.field|.''field'']] (or its variant [[#.5Bfield.5D|['''field''']]]) and [[#Array_Element_Selection|index-based selection]]. These two different forms are applied to JSON [[JSON_Concepts#Objects|Objects]] and [[JSON_Concepts#Arrays|Arrays]], respectively, 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: the dot notation [[#.field|.''field'']] (or its variant [[#.5Bfield.5D|['''field'' ']]]) and [[#Array_Element_Selection|index-based selection]]. These two different forms are applied to JSON [[JSON_Concepts#Objects|Objects]] and [[JSON_Concepts#Arrays|Arrays]], respectively, given the fact that JSON has only these two types of collections.


===Object Fields Selection===
===Object Fields Selection===

Revision as of 00:37, 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

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

[0-based-index]

The expression selects the element whose index is specified between square brackets from a JSON Array. The indexes are 0-based.

JSONPath Template