| 1 | ### v1.0.0 2020-09-28 |
| 2 | |
| 3 | ## Overview |
| 4 | This implementation is a subset of the intersection between [JSONPath](https://goessner.net/articles/JsonPath) and [YAML Path](https://pypi.org/project/yamlpath), and it focuses on addressing elements inside YAML files (node descriptors). There are no *query*-like capabilities. |
| 5 | |
| 6 | Example YAML structure: |
| 7 | ```yaml |
| 8 | foo: |
| 9 | - bar: &bar True |
| 10 | first: First Bar |
| 11 | second: 2 |
| 12 | arr: [1, 2, 3] |
| 13 | - baz: False |
| 14 | other_bar: *bar |
| 15 | first: First Baz |
| 16 | some.el/here: Delimiters... |
| 17 | "bar's": 0 |
| 18 | ``` |
| 19 | |
| 20 | Example JSON structure: |
| 21 | ```json |
| 22 | { |
| 23 | "foo": [ |
| 24 | { |
| 25 | "bar": true, |
| 26 | "first": "First Bar", |
| 27 | "second": 2, |
| 28 | "arr": [1, 2, 3] |
| 29 | }, |
| 30 | { |
| 31 | "baz": false, |
| 32 | "other_bar": true, |
| 33 | "first": "First Baz", |
| 34 | "some.el/here": "Delimiters...", |
| 35 | "bar's": 0, |
| 36 | } |
| 37 | ] |
| 38 | } |
| 39 | ``` |
| 40 | |
| 41 | ## Delimiters |
| 42 | Dot-notation (`.`) is the only supported notation to define *map key* path segments. Both *map key* and *sequence index* path segments could also be defined using square brackets (`[`, `]`). For *map key* and *map keys selection* segments both single (`'`) and double (`"`) quotes are supported, `['key']` is the same as `["key"]` and `['key',"other's key"]` is a valid path segment. |
| 43 | |
| 44 | For example: `$.foo[0].bar` or `.foo[0]['bar']` or `['foo'][0].bar` or `foo[1]["bar's"]`. |
| 45 | |
| 46 | ## Special symbols |
| 47 | A path might be prefixed by a dollar sign and a dot (`$`, `.`), but this prefix is retained for compatibility with *JSONPath* and not mandatory. Implicit *document root* is assumed unless the path explicitly starts with an *anchor* (`&...`) segment (see below for details). |
| 48 | |
| 49 | If the first segment is a *map key* segment (and explicit *document root* is omitted) the initial dot (`.`) is also not mandatory. |
| 50 | |
| 51 | For example, these paths are equal: `$.el`, `.el`, `el`, `['el']`. And they all address the value stored in the "el" key of the top-most map of the document. |
| 52 | |
| 53 | An asterisk (`*`) as a key name has a special meaning, and treated as an all-inclusive *keys selection* section (see below). That's it, `$.*` expression would include all keys of the map in the document root. The `[*]` syntax is also valid. One should use the `[:]` notation to acheive same effect for sequences (include all indices). |
| 54 | |
| 55 | ## Path Segment Types |
| 56 | |
| 57 | #### Document Root |
| 58 | `$` |
| 59 | |
| 60 | Optional explicit document root. Only allowed to appear at the beginning of the path. |
| 61 | |
| 62 | ```python |
| 63 | $.foo[0].bar = .foo[0].bar = foo[0].bar |
| 64 | == true |
| 65 | ``` |
| 66 | |
| 67 | |
| 68 | #### Map Key |
| 69 | `.map.key` or `.map['key']` |
| 70 | |
| 71 | ```python |
| 72 | $.foo[0].second = ['foo'][0]['second'] |
| 73 | == 2 |
| 74 | ``` |
| 75 | |
| 76 | |
| 77 | #### Map Keys Selection |
| 78 | `.map['key1','key2',...'keyN']` |
| 79 | |
| 80 | Special syntax for the all-inclusive key selection: `.*`. Also, there is the `[*]` variant of this syntax. |
| 81 | |
| 82 | ```python |
| 83 | $.foo[0]['first','second'] = ['foo'][0]['first','second'] |
| 84 | == {"first": "First Bar", "second": 2} |
| 85 | |
| 86 | foo[0]['first','second','bar','arr'] = foo[0].* |
| 87 | == {"bar": true, "first": "First Bar", "second": 2, "arr": [1, 2, 3]} |
| 88 | ``` |
| 89 | |
| 90 | |
| 91 | #### Sequence Index |
| 92 | `.array[<zero or positive number>]` |
| 93 | |
| 94 | ```python |
| 95 | $.foo[0] |
| 96 | == {"bar": True, "first": "First Bar", "second": 2} |
| 97 | ``` |
| 98 | |
| 99 | |
| 100 | #### Sequence Indices Set |
| 101 | `.array[<zero or positive number>,<zero or positive number>,...<zero or positive number>]` |
| 102 | |
| 103 | Special syntax for the all-inclusive indices set: `[:]`. |
| 104 | |
| 105 | ```python |
| 106 | $.foo[0].arr[0,1,2] = foo[0].arr[:] |
| 107 | == [1, 2, 3] |
| 108 | ``` |
| 109 | |
| 110 | |
| 111 | #### Anchor |
| 112 | `&anchor` |
| 113 | |
| 114 | Matches elements starting from the given anchor instead of the document root. This segment is only sensible in paths for YAML documents as there is no anchors/aliases concept in the JSON specification. |
| 115 | |
| 116 | ```python |
| 117 | $.foo[0].bar = &bar |
| 118 | == True |
| 119 | ``` |