• Stars
    star
    280
  • Rank 147,492 (Top 3 %)
  • Language
    C
  • License
    Eclipse Public Li...
  • Created almost 9 years ago
  • Updated almost 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Transform nested JSON data into tabular data in the shell.

json-table Build Status

Jt reads UTF-8 encoded JSON forms from stdin and writes tab separated values (or CSV) to stdout. A simple stack-based programming language is used to extract values from the JSON input for printing.

INSTALL

Linux users can install prebuilt binaries from the release tarball:

sudo bash -c "cd /usr/local && wget -O - https://github.com/micha/json-table/raw/master/jt.tar.gz | tar xzvf -"

macOS users can install the latest release using Homebrew:

brew install json-table

Otherwise, build from source:

make && make test && sudo make install

NOTE: Previous versions installed the jt manual in the $PREFIX/man/ directory, which was incorrect. They are now installed into $PREFIX/share/man/. If you have installed jt previously you will probably want to delete those old man pages from the $PREFIX/man/ directory if you install a newer version.

OVERVIEW

Extracting information from deeply nested JSON data is difficult and unreliable with tools like sed and awk, and tools that are specially designed for manipulating JSON are cumbersome to use in the shell because they either return their results as JSON or introduce a new turing complete scripting language that needs to be quoted and constructed via string interpolation.

Jt provides only what is needed to extract data from nested JSON data structures and organize the data into a table. Tools like cut, paste, join, sort, uniq, etc. can be used to efficiently reduce the tabular data to produce the final result.

Features

  • Self contained β€” statically linked, has no build or runtime dependencies.
  • Fast, small memory footprint β€” efficiently process large JSON input.
  • Correct β€” parser does not accept invalid JSON (see tests for details).

Example 1

Suppose we have some JSON data in a log file that we want to process:

{"account":123,"amount":1.00}
{"account":789,"amount":2.00}
{"account":123,"amount":3.00}
{"account":123,"amount":4.00}
{"account":456,"amount":5.00}

First, use jt to extract interesting values to get us out of JSON-world and into a nice tab delimited, newline separated tabular format that is amenable to processing with shell utilities:

jt [ account % ] amount % <<EOT
{"account":123,"amount":1.00}
{"account":789,"amount":2.00}
{"account":123,"amount":3.00}
{"account":123,"amount":4.00}
{"account":456,"amount":5.00}
EOT
123     1.00
789     2.00
123     3.00
123     4.00
456     5.00

From here we can process the values in the shell. For example, to compute the sum of the amounts for account 123:

jt [ account % ] amount % <<EOT | awk -F\\t '$1 == 123 {print $2}' | paste -sd+ |bc
{"account":123,"amount":1.00}
{"account":789,"amount":2.00}
{"account":123,"amount":4.00}
{"account":123,"amount":4.00}
{"account":456,"amount":5.00}
EOT
9.00

Or to compute the amount frequencies for the account:

jt [ account % ] amount % <<EOT | awk -F\\t '$1 == 123 {print $2}' | sort | uniq -c
{"account":123,"amount":1.00}
{"account":789,"amount":2.00}
{"account":123,"amount":4.00}
{"account":123,"amount":4.00}
{"account":456,"amount":5.00}
EOT
      1 1.00
      2 4.00

Example 2

Jt can also extract data from nested JSON:

jt asgs [ name % ] instances [ id % ] [ az % ] [ state % ] <<EOT
{
  "asgs": [
    {
      "name": "test1",
      "instances": [
        {"id": "i-9fb75dc", "az": "us-east-1a", "state": "InService"},
        {"id": "i-95393ba", "az": "us-east-1a", "state": "Terminating:Wait"},
        {"id": "i-241fd0b", "az": "us-east-1b", "state": "InService"}
      ]
    },
    {
      "name": "test2",
      "instances": [
        {"id": "i-4bbab16", "az": "us-east-1a", "state": "InService"},
        {"id": "i-417c312", "az": "us-east-1b", "state": "InService"}
      ]
    }
  ]
}
EOT
test1   i-9fb75dc       us-east-1a      InService
test1   i-95393ba       us-east-1a      Terminating:Wait
test1   i-241fd0b       us-east-1b      InService
test2   i-4bbab16       us-east-1a      InService
test2   i-417c312       us-east-1b      InService

The resulting TSV data can be piped to awk, for example, to get just the instances in test1 that are in service:

jt asgs [ name % ] instances [ id % ] [ az % ] [ state % ] <<EOT \
  | awk -F\\t '$1 == "test1" && $4 == "InService" {print}'
{
  "asgs": [
    {
      "name": "test1",
      "instances": [
        {"id": "i-9fb75dc", "az": "us-east-1a", "state": "InService"},
        {"id": "i-95393ba", "az": "us-east-1a", "state": "Terminating:Wait"},
        {"id": "i-241fd0b", "az": "us-east-1b", "state": "InService"}
      ]
    },
    {
      "name": "test2",
      "instances": [
        {"id": "i-4bbab16", "az": "us-east-1a", "state": "InService"},
        {"id": "i-417c312", "az": "us-east-1b", "state": "InService"}
      ]
    }
  ]
}
EOT
test1   i-9fb75dc       us-east-1a      InService
test1   i-241fd0b       us-east-1b      InService

DOCUMENTATION

See the man page or man jt in your terminal.

EXAMPLES

The man page has many examples.

SEE ALSO

Jt is based on ideas from the excellent jshon tool.

COPYRIGHT

Copyright Β© 2017 Micha Niskin. Distributed under the Eclipse Public License.

More Repositories

1

resty

Little command line REST client that you can use in pipelines (bash or zsh).
Shell
2,646
star
2

jsawk

Like awk, but for JSON.
Shell
1,385
star
3

imap-js

JavaScript clientside IMAP mail client written for the Golf web framework. Talks directly to an IMAP server using a long-polling socket proxy for transport.
JavaScript
31
star
4

jquery-eip

Edit-in-place plugin for jquery, uses tinymce.
JavaScript
10
star
5

declarify.js

UI kit js stuff
JavaScript
7
star
6

multi-module-build

Clojure
6
star
7

hlisp

HTML Lisp interpreter
JavaScript
6
star
8

jquery-tablesort

Tiny jQuery plugin to make tables sortable.
JavaScript
5
star
9

html-state-machine

A zero-programming, declarative state machine for html documents.
JavaScript
5
star
10

st

suckless.org simple terminal
C
4
star
11

golf-whitepaper

The Golf aaplication server whitepaper.
4
star
12

minimo-website

thinkminimo.com website
JavaScript
3
star
13

jquery-inlineLabels

Input text field labels with dimming action.
3
star
14

vim-config

My vim configurations.
Vim Script
3
star
15

golfdroid

Golf application development environment for the Google Android mobile platform.
Java
3
star
16

jquery-editmode

Plugin for creating modal editing interfaces in javascript.
3
star
17

dvtm

tiling window manager for the console
C
3
star
18

jquery-tableize

JQuery plugin for transforming data structure tables into HTML tables.
2
star
19

selectomatic.js

Abstraction of jQuery: use jQ's powerful collections patterns with your own selector engine and elements.
JavaScript
2
star
20

com.thinkminimo.getopt

A much simplified command line argument and options parser for java.
Java
2
star
21

p

stuff
2
star
22

longpoll

Asynchronous socket <--> long polling comet (HTTP) proxy servlet.
Java
2
star
23

trygolf

Write Golf apps online to try it out.
JavaScript
2
star
24

proa.me

The proa wiki site.
PHP
2
star
25

micha.github.com

my github pages
JavaScript
2
star
26

compy

The compy js frontend framework.
2
star
27

boot-cp

Classpath task for boot
Clojure
2
star
28

scanfillsendjs

testing 123
2
star
29

zmq-shell-utils

1
star
30

ss

Terminal spreadsheet program for engineering calcs.
Scheme
1
star
31

conix

Ship hull design web app, written for the Golf web framework.
JavaScript
1
star
32

jquery-sm-init

Initialization stuff for sm project js.
JavaScript
1
star
33

astronvim_config

Lua
1
star
34

js-libs

Clientside JavaScript/CoffeeScript libraries.
1
star
35

php-db-tools

Php mysql database tools and data access object framework.
1
star
36

letter

LaTeX letter template
1
star
37

todofrp

TodoMVC using HLisp and Javelin.
JavaScript
1
star
38

boot-uberjar-perf

Clojure
1
star
39

hlisp-test-site

Clojure
1
star
40

pilot-boat

Perl
1
star
41

xcompose

1
star
42

convore-simple

Simple ruby convore.com client. Incomplete, but simple.
Ruby
1
star
43

resume

My resume.
1
star
44

wigwam-example

An example web application using the wigwam php framework.
1
star