2017-11-30 5 views
1

は、私はノート(JSONを返すcurlコマンドJQのSTDアウトは常に

curl -H "Accept: application/json" https://icanhazdadjoke.com/ 

を持って失敗したファイルにしますほとんどのAPIはフォーマットなしのフラットjsonを返します... 1行)

私がJQにパイプすると、jqは期待どおりに応答します。 Iパイプ私はフォーマットされた読みやすいJSONを持って

curl -H "Accept: application/json" https://icanhazdadjoke.com/ | jq 

戻り

% Total % Received % Xferd Average Speed Time Time  Time Current 
           Dload Upload Total Spent Left Speed 
100 110 100 110 0  0 320  0 --:--:-- --:--:-- --:--:-- 321 
{ 
    "id": "NCAIYLeNe", 
    "joke": "I fear for the calendar, it’s days are numbered.", 
    "status": 200 
} 

しかし、私は、パイプJQの出力をテキストファイルに(私はフォーマットされたバージョンは、読みやすくするために保存したいを確保するために、JQします、ないプレーン未フォーマットJSON)私はエラーに

curl -H "Accept: application/json" https://icanhazdadjoke.com/ | jq > file.txt 

リターンを得る

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. 
    % Total % Received % Xferd Average Speed Time Time  Time Current 
           Dload Upload Total Spent Left Speed 
100 141 100 141 0  0 317  0 --:--:-- --:--:-- --:--:-- 316 
(23) Failed writing body 
あなたは jqは、それが入力として得た同じJSONをフォーマットし、それを実行するためにスクリプトとして .を渡したい場合は
+0

つまり、 'icanhazdadjoke'の出力は実際には1行として返されません。あなたはその質問にいくつかの行を表示しています。私はそれを自分で実行すると、いくつかの行(すでにきれいに印刷されています)に渡しています。 –

+0

はい、私の質問の注記のセクションを読んだら、このAPIはきれいなプリントを返すと言います。私は、APIを理解することはできませんが、APIを理解していないaphedの上で働いていますstackoverflowのためのより良い – Goldfish

+0

Gotcha。私は、他人が使うことができる再生器を作るためのあなたの努力に感謝します。将来のメモとして、http://ix.io/はhttp://sprunge.us/です。これは、ファイルをパイプして人々がまったく同じビットを得ることを可能にする、文書化された 'curl'に優しいインターフェースを持つ広告なしペーストビンですバック;この種の例には便利です。かなり確信してhttps://gist.github.com/はあまりにも生のインターフェースを持っていますが、(IX/sprungeドキュメント彼らのアップフロントのに対し)を見なければならないでしょう。その標準出力が端末である場合jq' 'の呼び出しがフィルタを省略することができ –

答えて

3

curl -H "Accept: application/json" https://icanhazdadjoke.com/ | jq . > file.txt 

the manualから:

アイデンティティ:.

最も単純なフィルタは.です。これは入力を受け取り、出力としてそのまま出力するフィルタです。つまり、これはアイデンティティ演算子です。

デフォルトでは、すべての出力がきれいに印刷されるため、この簡単なプログラムは、JSON出力を書式設定する便利な方法です(curlなど)。

+0

非常に素晴らしい!あなたはマニュアルまたは '.'はhttps://stedolan.github.io/jq/manual/で – Goldfish

+0

を何の説明、「アイデンティティ」の検索へのリンクを提供することができます。 –

+2

は、あなたの質問に使用方法のメッセージを確認しますかわいいプリンター。 – Aaron