share facebook facebook2 twitter menu hatena pocket slack

2017.05.10 WED

小ネタ道場一本勝負 〜 俺にその JSON を一行でくれよ 〜

川原 洋平

WRITTEN BY 川原 洋平

その JSON を…

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "name": "name"
          }
        },
        {
          "term": {
            "description": "keyword"
          }
        },
        {
          "term": {
            "address": "location"
          }
        }
      ]
    }
  }
}

一行で欲しい時がある。

ということで、一本

jq の -c オプションを使えば一瞬で。

$ echo '{
>   "query": {
>     "bool": {
>       "must": [
>         {
>           "term": {
>             "name": "name"
>           }
>         },
>         {
>           "term": {
>             "description": "keyword"
>           }
>         },
>         {
>           "term": {
>             "address": "location"
>           }
>         }
>       ]
>     }
>   }
> }' | jq -c
{"query":{"bool":{"must":[{"term":{"name":"name"}},{"term":{"description":"keyword"}},{"term":{"address":"location"}}]}}}

意図した通りに一行で頂きました。

{"query":{"bool":{"must":[{"term":{"name":"name"}},{"term":{"description":"keyword"}},{"term":{"address":"location"}}]}}}

ありがとう。

あざっした

$ jq --version
jq-1.5

$ jq --help
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.

元記事はこちら

小ネタ道場一本勝負 〜 俺にその JSON を一行でくれよ 〜