NAME

jt - JSON Transformer

VERSION

version 0.43

SYNOPSIS

jt works only with data from input pipe. Here are some examples. For quick references on command line options, run jt -h. For more comprehensive documentation, run perldoc =jt.

# prettyfied
curl http://example.com/action.json | jt

# uglified
cat random.json | jt --ugly > random.min.json

# take only selected fields
cat cities.json | jt --field name,country,latlon

## --pick, --grep, -map assumes the input is an array.
# randomly pick 10 hashes
cat cities.json | jt --pick 10

# pick 10 hashes from position 100, and uglified the output
cat cities.json | jt --pick 100..109 --ugly

# filtered by code
cat cities.json | jt --grep '$_{country} eq "us"' | jt --field name,latlon

# convert to csv. Only scalar values are chosen.
cat cities.json | jt --csv

# Run a piece of code on each hash
cat orders.json | jt --map 'say "$_{name} sub-total: " . $_{count} * $_{price}'

DESCRIPTION

jt assumes the input is some data serialized as JSON, and perform transformation based on its parameter. It can be used to deal with various RESTful web service api, such as ElasticSearch.

OUTPUT OPTIONS

The default output format is JSON. If --csv is provided then simple fields are chosen and then converted to CSV. If --tsv is provided then it becomes tab-separated values.

SELECTING FIELDS

The --field option can be used to select only the wanted fields in the output.

The field name notation is based on Hash::Flatten or MongoDB. "." is used to delimit sub-fields within a hash, and ":" is used to delimit array elements. Here's a brief example table that maps such flatten notation with perl expression:

| flatten notation | perl expression        |
|                  |                        |
| foo.bar          | $_->{foo}{bar}         |
| foo:0            | $_->{foo}[0]           |
| foo.bar:3.baz    | $_->{foo}{bar}[3]{baz} |
| foo.0.bar.4      | $_->{foo}{0}{bar}{4}   |

The --fields option transform the input such that the output contain only values of those fields. It may contain multilpe values seperated by comma, such as:

--fields title,address,phone

Each specified field name is matched to the flatten notation of full field names. So "title" would match any "title" field an any depth in the input.

Then the input is an array of hash, then it applies on the hashes inside, so the selection can be simplified.

SELECTING BY JSON PATH

JSONPath is a more comprehensive way to specify the elements within a JSON structure. See the full description of JSONPath at http://goessner.net/articles/JsonPath/

To specify a JSONPath selector, use --json-path option. Here are some simple examples:

cat store.json | jt --json-path '$..author'   # Get all book authors
cat store.json | jt --json-path '$..book[:2]' # The first 2 books.

# print unique authors
cat store.json | bin/jt --json-path '$..author' | bin/jt --silent --map 'say $_' | sort -u

AUTHOR

Kang-min Liu <gugod@gugod.org>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2013 by Kang-min Liu.

This is free software, licensed under:

The MIT (X11) License