App::yajg - yet another json grep
yajg [-cEhimquvz] [-p key_pattern] [-P value_pattern] [-s select_path] [-S select_path] [-o output_format] [-b boolean_type] [-d depth] [--sort-keys] [-e code] [files]
Simple grep and pretty output for json in each files or standard input.
Perl regexp pattern for matching hash keys.
Perl regexp pattern for matching array or hash values.
WARNING can change number type to string.
Interpret pattern given in the "-p, --key-pattern" or the "-P, --value-pattern" options as the substring (calls perl quotemeta for pattern).
quotemeta
Select the element at the structure for grep by the given path. For example:
yajg -s {rows}.[0,1,2]./id|title/
If there are no "-p, --key-pattern" or "-P, --value-pattern" options provided, the yajg will dump the full structure by the path. The path must be dot-separated (.) string which can contains the following elements:
.
If the element of the path in braces ({...}) will try to select by key. Only supported by hash types.
{...}
If the element of the path in brackets ([...]) will try to select the element by array slice (@{$data}[ elements ]). The element must be an integer value or comma-separated list of integer values.
[...]
@{$data}[ elements ]
If the element of the path between / (/.../) will try to select elements by keys/indexes that matches given regexp. For example /\d+/ will match the hole array or hash keys that are positive integer numbers.
/
/.../
/\d+/
If the element has no special symbols at the begin and end will try to select elements by key or index (depends on data)
If you want to path dot as the element symbol - you must escape it with \. For example: {data\.s}./^rt\.*/ means to select element by key data.s and then all elements which keys matches regexp m/^rt.*/
\
data.s
m/^rt.*/
Same as the "-s, --select" but will try to tiny output: will go throw the data and while data is array or hash with one element this element will be data. For example:
$ echo '[{"1":1},{"2":2},{"3":3}]' | yajg -S '1' {"2":2} $ echo '[{"1":1},{"2":2},{"3":3}]' | yajg -S -s '1' {"2":2}
Ignore case distinctions in the "-p, --key-pattern", the "-P, --value-pattern" and "-s, --select" options.
Invert the sense of matching for the "-P, --value-pattern" option.
Select the output type. Supported types are:
json (via JSON)
perl (via Data::Dumper)
ddp (via Data::Printer)
yaml (via YAML)
If the Data::Printer installed than the default value will be ddp otherwise json.
Convert boolean types to defined format:
ref, 1 - ref to scalar \0, \1
\0
\1
int, 2 - integer 0, 1
0
1
str, 3 - string 'false', 'true'
'false'
'true'
Maby usefull because by default all true is ref to Types::Serialiser::true and all false is ref to Types::Serialiser::false and the output in some formats can be hard to read
true
Types::Serialiser::true
false
Types::Serialiser::false
Enable/disable colorized output. For json and perl output types you need to install the highlight program.
Print or hide filenames. By default print filenames if there are more than one files.
How deep to traverse the data (0 for all)
WARNING when the json booleans has same level as the --max-depth then they will be converted to string 0, 1 or true, false (depends on JSON version)
Minimize output. Does not supported by the yaml output.
Enable/disable sorting hash keys. By default enabled when the "-m, --minimal" option is disabled.
Print non-printable chars as "\n", "\t", etc. By default enabled when the "-m, --minimal" option is enabled. For json output always enabled (JSON format requires to escape this chars)
Quiet; do not write anything to standard output.
Try to parse urls. Will be called after selection and filtering.
Evaluate perl code on every item wich is niether hash nor array ref. Will be called after selection and filtering. The item data that has been written is in $_ and whatever is in there is written out afterwards.
$_
Display short help message
Normally the exit status is 0 if the any structure has size, 1 if no structures has size, and 2 if an error occurred.
exaple.json
{ "array" : [ { "data" : { "a" : 1, "b" : 2 }, "id" : "test" }, { "data" : { "a" : 100, "b" : 200 }, "id" : "test_2" } ], "hash" : { "numbers" : { "one" : 1, "three" : 3 }, "words" : [ "cat", "dog", "bird" ] } }
$ yajg -p id exaple.json { "array" : [ { "id" : "test" }, { "id" : "test_2" } ] } $ yajg -p id -o perl exaple.json { 'array' => [ { 'id' => 'test' }, { 'id' => 'test_2' } ] } $ yajg -p id -o ddp exaple.json \ { array [ [0] { id "test" }, [1] { id "test_2" } ] } $ yajg -p id -o yaml exaple.json --- array: - id: test - id: test_2
yajg -P '^1$' exaple.json { "array" : [ { "data" : { "a" : "1" } } ], "hash" : { "numbers" : { "one" : "1" } } } $ yajg -P 2 -p id exaple.json { "array" : [ { "id" : "test_2" } ] } $ yajg -P 'cat|dog' exaple.json { "hash" : { "words" : [ "cat", "dog" ] } }
Simple selection:
$ yajg -s hash.words.0 exaple.json { "hash" : { "words" : [ "cat" ] } }
words not a hash:
words
$ yajg -s {hash}.{words}.{0} exaple.json {}
Last element of words:
$ yajg -s hash.words.[-1] exaple.json { "hash" : { "words" : [ "bird" ] } }
First and third element of words:
$ yajg -s hash.words.[0,2] exaple.json { "hash" : { "words" : [ "cat", "bird" ] } }
Slice on hash will be empty:
$ yajg -s hash.[0] exaple.json {}
Regexp example:
$ yajg -s 'array./\d+/.id' exaple.json { "array" : [ { "id" : "test" }, { "id" : "test_2" } ] } $ yajg -s '/\.*/.numbers./^o/' exaple.json { "hash" : { "numbers" : { "one" : 1 } } }
$ yajg -s 'array.0' -P 1 exaple.json { "array" : [ { "data" : { "a" : "1" } } ] }
$ yajg -d 2 -o json exaple.json { "array" : [ "HASH(0x1d239b8)", "HASH(0x1ddb958)" ], "hash" : { "numbers" : "HASH(0x1ef51a0)", "words" : "ARRAY(0x1ef5218)" } } $ yajg -d 2 -o perl exaple.json { 'array' => [ 'HASH(0xf87dc0)', 'HASH(0xf87b38)' ], 'hash' => { 'numbers' => 'HASH(0xf87d78)', 'words' => 'ARRAY(0x7a93d0)' } } $ yajg -d 2 -o ddp exaple.json \ { array [ [0] { ... }, [1] { ... } ], hash { numbers { ... }, words [ ... ] } }
$ echo '[1,2,3]' | yajg -e '$_+=1' -m [2,3,4] $ echo '{"a":1,"b":2}' | yajg -e '$_+=1' -e '$_*=2' { "a" : 4, "b" : 6 }
Copyright 2017 Grigoriy Koudrenko <gragory.mail@gmail.com>.
<gragory.mail@gmail.com>
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.
The full text of the license can be found in the LICENSE file included with this program.
To install App::yajg, copy and paste the appropriate command in to your terminal.
cpanm
cpanm App::yajg
CPAN shell
perl -MCPAN -e shell install App::yajg
For more information on module installation, please visit the detailed CPAN module installation guide.