NAME

App::yajg - yet another json grep.

SYNOPSIS

yajg [-chimquvz] [-p key_pattern] [-P value_pattern] [-s select_path] [-S select_path] [-o output_format] [-b boolean_type] [-d depth] [-e code] [files]

DESCRIPTION

Simple grep and pretty output for json in each files or standard input.

OPTIONS

Grep control

-p, --key-pattern

Perl regexp pattern for matching hash keys.

-P, --value-pattern

Perl regexp pattern for matching array or hash values.

WARNING can change number type to string.

-z, --substring

Interpret pattern given in the "-p, --key-pattern" or the "-P, --value-pattern" options as the substring (calls perl quotemeta for pattern).

-s, --select

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:

{HASH KEY}

If the element of the path in braces ({...}) will try to select by key. Only supported by hash types.

[SLICE]

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.

/REGEXP/

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.

UNKNOWN

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.*/

-S, --select-tiny

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}
-i, --ignore-case

Ignore case distinctions in the "-p, --key-pattern", the "-P, --value-pattern" and "-s, --select" options.

-v, --invert-match

Invert the sense of matching for the "-P, --value-pattern" option.

Output control

-o, --output

Select the output type. Supported types are:

If the Data::Printer installed than the default value will be ddp otherwise json.

-b, --boolean

Convert boolean types to defined format:

  • ref, 1 - ref to scalar \0, \1

  • int, 2 - integer 0, 1

  • str, 3 - string '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 the perl or the ddp format looks very ugly.

-c, --color, --no-color

Enable/disable colorized output. For json and perl output types you need to install the highlight program.

--filename, --no-filename

Print or hide filenames. By default print filenames if there are more than one files.

-d, --max-depth

How deep to traverse the data (0 for all)

-m, --minimal

Minimize output. Supported only by json and perl output types.

-q, --quiet

Quiet; do not write anything to standard output.

-u, --url-parse

Try to parse urls. Will be called after selection and filtering.

WARNING can change number type to string.

Miscellaneous

-e, --exec

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.

-h, --help

Display short help message

EXIT STATUS

Normally the exit status is 0 if the any structure has size, 1 if no structures has size, and 2 if an error occurred.

EXAMPLES

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"
      ]
   }
}

Key grep and different output format

$ 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"
        }
    ]
}

Value grep

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"
      ]
   }
}

Select option

Simple selection:

$ yajg -s hash.words.0 exaple.json
{
   "hash" : {
      "words" : [
         "cat"
      ]
   }
}

words not a hash:

$ 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
      }
   }
}

Select with grep

$ yajg -s 'array.0' -P 1 exaple.json
{
   "array" : [
      {
         "data" : {
            "a" : "1"
         }
      }
   ]
}

Max depth

$ 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     [ ... ]
    }
}

exec

$ 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
}

SEE ALSO

JSON::XS
Data::Dumper
Data::Printer
highlight(1)

LICENSE AND COPYRIGHT

Copyright 2017 Grigoriy Koudrenko <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.