mirror of
https://github.com/stedolan/jq.git
synced 2024-05-11 05:55:39 +00:00
Support ."foo" syntax for accessing fields. See #141.
This commit is contained in:
@@ -160,10 +160,13 @@ sections:
|
|||||||
- title: "`.foo`"
|
- title: "`.foo`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
The simplest *useful* filter is .foo. When given a
|
The simplest *useful* filter is `.foo`. When given a
|
||||||
JSON object (aka dictionary or hash) as input, it produces
|
JSON object (aka dictionary or hash) as input, it produces
|
||||||
the value at the key "foo", or null if there's none present.
|
the value at the key "foo", or null if there's none present.
|
||||||
|
|
||||||
|
If the key contains special characters, you need to surround
|
||||||
|
it with double quotes like this: `."foo$"`.
|
||||||
|
|
||||||
examples:
|
examples:
|
||||||
- program: '.foo'
|
- program: '.foo'
|
||||||
input: '{"foo": 42, "bar": "less interesting data"}'
|
input: '{"foo": 42, "bar": "less interesting data"}'
|
||||||
@@ -171,6 +174,9 @@ sections:
|
|||||||
- program: '.foo'
|
- program: '.foo'
|
||||||
input: '{"notfoo": true, "alsonotfoo": false}'
|
input: '{"notfoo": true, "alsonotfoo": false}'
|
||||||
output: ['null']
|
output: ['null']
|
||||||
|
- program: '."foo"'
|
||||||
|
input: '{"foo": 42}'
|
||||||
|
output: [42]
|
||||||
|
|
||||||
- title: "`.[foo]`, `.[2]`, `.[10:15]`"
|
- title: "`.[foo]`, `.[2]`, `.[10:15]`"
|
||||||
body: |
|
body: |
|
||||||
|
6
parser.y
6
parser.y
@@ -406,6 +406,12 @@ Term FIELD {
|
|||||||
FIELD {
|
FIELD {
|
||||||
$$ = gen_index(gen_noop(), gen_const($1));
|
$$ = gen_index(gen_noop(), gen_const($1));
|
||||||
} |
|
} |
|
||||||
|
Term '.' String {
|
||||||
|
$$ = gen_index($1, $3);
|
||||||
|
} |
|
||||||
|
'.' String {
|
||||||
|
$$ = gen_index(gen_noop(), $2);
|
||||||
|
} |
|
||||||
'.' error {
|
'.' error {
|
||||||
FAIL(@$, "try .[\"field\"] instead of .field for unusually named fields");
|
FAIL(@$, "try .[\"field\"] instead of .field for unusually named fields");
|
||||||
$$ = gen_noop();
|
$$ = gen_noop();
|
||||||
|
@@ -115,6 +115,10 @@ null
|
|||||||
{"foo": {"bar": 42}, "bar": "badvalue"}
|
{"foo": {"bar": 42}, "bar": "badvalue"}
|
||||||
42
|
42
|
||||||
|
|
||||||
|
."foo"."bar"
|
||||||
|
{"foo": {"bar": 20}}
|
||||||
|
20
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Multiple outputs, iteration
|
# Multiple outputs, iteration
|
||||||
|
Reference in New Issue
Block a user