-
Notifications
You must be signed in to change notification settings - Fork 4
Selection Path Syntax
Thiago Souza edited this page Apr 8, 2018
·
5 revisions
To give a general idea, yajs
selection path is heavly inspired on jsonpath while being a subset of it (i.e. not everything is supported).
-
Root Object: Every
yajs
path starts with the$
root object reference, for instance:
Example: yajs '$'
$ echo '{ "prop1": "value1" }' | yajs '$'
{"prop1":"value1"}
-
Path Separator: Property path selections are separated by a
.
:
Example: yajs '$.prop1.prop2'
$ echo '{ "prop1": { "prop2": "nested" } }' | yajs '$.prop1.prop2'
"nested"
-
Array Values: The syntax does not changes for an array value so a
.
should be used to address the properties of the object values (in contrast withjsonpath
syntax):
Example: yajs '$.prop1.prop2'
$ echo '{ "prop1": [ { "prop2": "nested1" }, { "prop2": "nested2" } ] }' | yajs '$.prop1.prop2'
"nested1"
"nested2"
Note: this also means that there is no way to address an specific index inside an array.
-
Deep Nested Values: Deep nested values can be addressed using double
.
notation, without the need to specify the actual property path:
Example: yajs '$..prop3'
$ echo '{ "prop1": { "prop2": { "prop3": "nested" } } }' | yajs '$..prop3'
"nested"
- Path Filter Expression: A boolean expression can be combined with deep nested syntax so values will be selected only if it is addressed by a set o parent property keys that satisfies the boolean expression:
Example: yajs '$..[prop1 || prop2]prop3'
$ echo '[ { "prop1": { "prop3": "nested1" } }, { "prop2": { "prop3": "nested2" } }, { "skipped": { "prop3": "nested1" } } ]' | yajs '$..[prop1 || prop2]prop3'
"nested1"
"nested2"
- Property Filter Expression: A boolean expression can be combined with deep nested syntax so values will be selected only if the value is an object that contains a set of property keys that satisfies the boolean expression:
Example: yajs '$..prop1{nested1 || nested2}'
$ echo '[ { "prop1": { "nested1": "value1" } }, { "prop1": { "nested2": "value2" } }, { "skipped": { "nested3": "value3" } } ]' | yajs '$..prop1{nested1 || nested2}'
{"nested1":"value1"}
{"nested2":"value2"}
Note: this is only currently supported as being the last component of the yajs
path.
-
Wildcard: A wildcard
*
can be used to address values in which the property is unknown:
Example yajs '$.prop1.*'
$ echo '{ "prop1": [ { "prop2": "nested1" }, { "random": "nested2" }, { "unknown": "nested3" } ] }' | yajs '$.prop1.*'
"nested1"
"nested2"
"nested3"