一行で出力されているjsonデータを整形して結果をファイルにリダイレクトしようと以下のようなコマンドを打ったのですが、helpが表示されてリダイレクトされません。
$ cat sample.json
{"id": "0001", "type": "donut", "name": "Cake", "ppu": 0.55, "batters": {"batter": [{"id": "1001", "type": "Regular"},{"id": "1002", "type": "Chocolate"},{"id": "1003", "type": "Blueberry"},{"id": "1004", "type": "Devil's Food"}]}, "topping": [{"id": "5001", "type": "None"},{"id": "5002", "type": "Glazed"},{"id": "5005", "type": "Sugar"},{"id": "5007", "type": "Powdered Sugar"},{"id": "5006", "type": "Chocolate with Sprinkles"},{"id": "5003", "type": "Chocolate"},{"id": "5004", "type": "Maple"}]}
$ cat sample.json | jq > formatted.json
jq - commandline JSON processor [version 1.5]
Usage: jq [options] <jq filter> [file...]
jq is a tool for processing JSON inputs, applying the
given filter to its JSON text inputs and producing the
filter's results as JSON on standard output.
The simplest filter is ., which is the identity filter,
copying jq's input to its output unmodified (except for
formatting).
For more advanced filters see the jq(1) manpage ("man jq")
and/or https://stedolan.github.io/jq
Some of the options include:
-c compact instead of pretty-printed output;
-n use `null` as the single input value;
-e set the exit status code based on the output;
-s read (slurp) all inputs into an array; apply filter to it;
-r output raw strings, not JSON texts;
-R read raw strings, not JSON texts;
-C colorize JSON;
-M monochrome (don't colorize JSON);
-S sort keys of objects on output;
--tab use tabs for indentation;
--arg a v set variable $a to value <v>;
--argjson a v set variable $a to JSON value <v>;
--slurpfile a f set variable $a to an array of JSON texts read from <f>;
See the manpage for more options.
$
2, 3回首をかしげながら同じコマンドをうったあと、よくhelpを見てみるとこう記載されています。
Usage: jq [options] <jq filter> [file...]
jq is a tool for processing JSON inputs, applying the
given filter to its JSON text inputs and producing the
filter's results as JSON on standard output.
The simplest filter is ., which is the identity filter,
copying jq's input to its output unmodified (except for
formatting).
意訳すると、「jqは入力されたjsonを与えられたfilterを適用して結果を標準出力に出力します。一番簡単なfilterは整形だけして出力する.です。」といったところでしょうか。
Usageに
Usage: jq [options] <jq filter> [file...]
となっているので以下のようなコマンドを打ってみます。
$ jq . sample.json > formatted.json
$ cat formatted.json
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters": {
"batter": [
{
"id": "1001",
"type": "Regular"
},
{
"id": "1002",
"type": "Chocolate"
},
{
"id": "1003",
"type": "Blueberry"
},
{
"id": "1004",
"type": "Devil's Food"
}
]
},
"topping": [
{
"id": "5001",
"type": "None"
},
{
"id": "5002",
"type": "Glazed"
},
{
"id": "5005",
"type": "Sugar"
},
{
"id": "5007",
"type": "Powdered Sugar"
},
{
"id": "5006",
"type": "Chocolate with Sprinkles"
},
{
"id": "5003",
"type": "Chocolate"
},
{
"id": "5004",
"type": "Maple"
}
]
}
$
うまくいきました。ちなみにfilterはこんなふうに使うみたいですね。また一つ勉強になりました。