Ten command line tools for manipulate JSON arrays
It's not a mystery: raw JSON it's not really readable!
So, when you need to process a JSON array from command line, it's a good idea using some helpful tools.
Here my own shortlist of command line tools for JSON manipulation.
validjson
Command line tool to validate JSON syntax of input files.
Example
$ validjson file.json
OK: file.json
$ curl http://site.com/file.json | validjson
OK: -
Download
jsonwatch
Command line utility to track changes in JSON data delivered by a shell command or a web, similar to watch command with the -d switch.
Example
$ jsonwatch -u http://api.openweathermap.org/data/2.5/weather\?q\=Kiev,ua --no-initial-values -n 300
2014-03-17T23:06:19.073790
+ .rain.1h: 0.76
- .rain.3h: 0.5
.dt: 1395086402 -> 1395089402
.main.temp: 279.07 -> 278.66
.main.temp_max: 279.82 -> 280.15
.main.temp_min: 277.95 -> 276.05
.sys.message: 0.0353 -> 0.0083
Download
fx
Command-line JSON processing tool, able to manage JSON from stdin and process it with an anonymous function.
Example
$ echo '{"foo": [{"bar": "value"}]}' | fx 'x => x.foo[0].bar'
"value"
Download
gron
Utility that transforms JSON into discrete assignments to make it easier to grep.
Example
$ gron "https://api.github.com/repos/tomnomnom/gron/commits?per_page=1" | fgrep "commit.author" json[0].commit.author = {}; json[0].commit.author.date = "2016-07-02T10:51:21Z"; json[0].commit.author.email = "mail@tomnomnom.com"; json[0].commit.author.name = "Tom Hudson";
Download
jl
"JSON lambda" is a functional language for querying and manipulating JSON
Example
$ jl 'map $ \o -> { sha: o.sha, ps: map _.sha o.parents }' x.json
[{"sha":"7b81a836c31500e685d043729259affa8b670a87","ps":["c538237f4e4c381d35f1c15497c...
Download
jo
Command-line utility to create JSON objects.
Example
$ jo -p name=jo n=17 parser=false
{
"name": "jo",
"n": 17,
"parser": false
}
Download
json-table
Reads UTF-8 encoded JSON forms from stdin and writes tab separated values (or CSV) to stdout.
Example
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
Download
json
Node.js CLI tool for working with JSON.
Example
$ echo '{"foo":"bar"}' | json
{
"foo": "bar"
}
$ echo '{"foo":"bar"}' | json foo
bar
$ echo '{"fred":{"age":42}}' | json fred.age # '.' for property access
42
$ echo '{"age":10}' | json -e 'this.age++'
{
"age": 11
}
# `json -ga` (g == group, a == array) for streaming mode
$ echo '{"latency":32,"req":"POST /widgets"}
{"latency":10,"req":"GET /ping"}
' | json -gac 'this.latency > 10' req
POST /widgets
Download
jsonaxe
Command-line JSON processor written in Python.
Example
$ ./jsonaxe 'objects' test/data.json
{
"a": {
"b": {
"c": "C value"
}
}
}
# nested objects can be traversed using dot "."
$ ./jsonaxe 'objects.a.b.c' test/data.json
"C value"
# square-bracket object["key"] syntax works too
$ ./jsonaxe 'objects["a"]["b"]["c"]' test/data.json
"C value"
# python-style string expressions can drill down to a specific character
$ ./jsonaxe 'objects.a.b.c[0]' test/data.json
"C"
# the full slice syntax can be used, e.g. every 2nd character from 0-5
$ ./jsonaxe 'objects.a.b.c[0:5:2]' test/data.json
"Cvl"
# manipulate object values in-place
$ ./jsonaxe 'objects.a.b.put("d", 42)' test/data.json
{
"c": "C value",
"d": 42
}
Download
jp
Command line interface to JMESPath, an expression language for manipulating JSON.
Example
$ echo '{"foo": {"bar": ["a", "b", "c"]}}' | jp foo.bar[1]
"b"