JSONファイルをjqでギュッとしたい

## あ、あのぉ…、JSONをギュッとしたいって？

えっ？わかりません？

つまり、JSONを、こう…ギュッ！とするってことですよ。

…あ、ほらほら。目の前にちょうど都合よく、野生のJSONがいるじゃないですか。

このJSONを暴れないよう、こう足を掴んで捕まえて、その後、両手でこう、優しく包み込むように持ちますね。

そのあと、ここの辺りを、こう思い切って、ギュッ！っと握ってみてください！ギュッと！
ああ、怖がらなくて大丈夫！
ギュッとしたとき、グボッとなって、手はちょっと汚れますが、石鹸で洗えば落ちますから。

ほら、勇気をだして、ギュッと！ギュッと！

ほらほら！ギュッ！と…
おっ！JSONを、ギュッとできました～。
やればできるじゃーないですか…

…といったコントじみた小芝居はさておき、つまりこういうことです。

## JSONをギュッとする

こういう野生のJSONを
```JSON
{
  "11001": {
    "type": "C",
    "elems": "11112010",
    "lat": [
      45,
      31.2
    ],
    "lon": [
      141,
      56.1
    ],
    "alt": 26,
    "kjName": "宗谷岬",
    "knName": "ソウヤミサキ",
    "enName": "Cape Soya"
  },
  "11016": {
    "type": "A",
    "elems": "11111111",
    "lat": [
      45,
      24.9
    ],
    "lon": [
      141,
      40.7
    ],
    "alt": 3,
    "kjName": "稚内",
    "knName": "ワッカナイ",
    "enName": "Wakkanai"
  },
  "11046": {
    "type": "C",
    "elems": "11112010",
...
```

こうする
```JSON
{"11001":{"type":"C","elems":"11112010","lat":[45,31.2],"lon":[141,56.1],"alt":26,"kjName":"宗谷岬","knName":"ソウヤミサキ","enName":"Cape Soya"},"11016":{"type":"A","elems":"11111111","lat":[45,24.9],"lon":[141,40.7],"alt":3,"kjName":"稚内","knName":"ワッカナイ","enName":"Wakkanai"},"11046":{"type":"C","elems":"11112010",
...
```
ってことです。

人間様が読むにはギュッとしてない状態でいいんですが、これはこれでコピペしたりするときに何行も選択するのがめんどくさいですね。

しかし、ギュッとするのに、エディタなんかでちまちま手修正するのはめんどくさいですね。

めんどくさいのが嫌いなエンジニアとしては、なんか楽したいですよねぇ…

## そこでjqコマンド

ということで、JSONの整形に使える便利コマンド・おなじみの jq を使います。

なんと、jq はオプション次第で、読みやすく整形するだけでなく、1ライナーなJSONにもしてくれるんですね。

整形したいときは

```
$ cat amedasstable.json | jq . 
```

こうなりますが、

```
{
  "11001": {
    "type": "C",
    "elems": "11112010",
    "lat": [
      45,
      31.2
    ],
    "lon": [
      141,
      56.1
    ],
    "alt": 26,
    "kjName": "宗谷岬",
    "knName": "ソウヤミサキ",
    "enName": "Cape Soya"
  },
  "11016": {
    "type": "A",
    "elems": "11111111",
    "lat": [
      45,
      24.9
    ],
    "lon": [
      141,
      40.7
    ],
    "alt": 3,
    "kjName": "稚内",
    "knName": "ワッカナイ",
    "enName": "Wakkanai"
  },
  "11046": {
    "type": "C",
    "elems": "11112010",
```

1ライナーのJSONにしたいときは、-c のオプションつけて

```
cat amedasstable.json | jq -c . 
```

```
ubuntu@DESKTOP-SUZUKIWIN:/mnt/c/Users/mieru/Downloads$ cat amedasstable.json | jq . -c | more                                          {"11001":{"type":"C","elems":"11112010","lat":[45,31.2],"lon":[141,56.1],"alt":26,"kjName":"宗谷岬","knName":"ソウヤミサキ","enName":"Cape Soya"},"11016":{"type":"A","elems":"11111111","lat":[45,24.9],"lon":[141,40.7],"alt":3,"kjName":"稚内","knName":"ワッカナイ","enName":"Wakkanai"},"11046":{"type":"C","elems":"11112010","lat":[45,18.3],"lon":[141,2.7],"alt":65,"kjName":"礼文","knName":"レブン","enName":"Rebun"},"11061":{"type":"C","elems":"11110100","lat":[45,24.2],"lon":[141,48.1],"alt":8,"kjName":"声問","knName":"コエトイ：稚内空 ","enName":"Koetoi"},"11076":{"type":"C","elems":"11112110","lat":[45,20.1],"lon":[142,10.2],"alt":13,"kjName":"浜鬼志別","knName":"ハ
オニシベツ","enName":"Hamaoni-Shibetsu"},"11091":{"type":"C","elems":"11110000","lat":[45,14.5],"lon":[141,11.2],"alt":30,"kjName":"本
","knName":"モトドマリ：利尻空港","enName":"Motodomari"},"11121":{"type":"C","elems":"11112100","lat":[45,14.9],"lon":[141,51.1],"alt":23,"kjName":"沼川","knName":"ヌマカワ","enName":"Numakawa"},"11151":{"type":"C","elems":"11112010","lat":[45,10.7],"lon":[141,8.3],"alt":14,"kjName":"沓形","knName":"クツガタ","enName":"Kutsugata"},"11176":{"type":"C","elems":"11112100","lat":[45,6.1],"lon":[141,46.8],"alt":14,"kjName":"豊富","knName":"トヨトミ","enName":"Toyotomi"},"11206":{"type":"C","elems":"11112000","lat":[45,7.5],"lon":[142,21],"alt":18,"kjName":"浜頓別","knName":"ハマトンベツ","enName":"Hamatonbetsu"},"11276":{"type":"C","elems":"11112100","lat":[44,57.9],"lon":[142,16.8],"alt":25,"kjName":"中頓別","knName":"ナカトンベツ","enName":"Nakatonbetsu"},"11291":{"type":"B","elems":"11111111","lat":[44,56.4],"lon":[142,35.1],"alt":7,"kjName":"北見枝幸","knName":"キタミエサシ","enName":"Kitami-Esashi"},"11316":{"type":"C","elems":"11112100","lat":[44,50.5],"lon":[142,28.8],"alt":14,"kjName":"歌登","knName":"ウタノボリ","enName":"Utanobori"},"12011":{"type":"C","elems":"11112000","lat":[44,49.7],"lon":[142,4.6],"alt":22,"kjName":"中川","knName":"ナカガワ","enName":"Nakagawa"},"12041":{"type":"C","elems":"11112110","lat":[44,43.6],"lon":[142,15.9],"alt":40,"kjName":"音威子府","knName":"オトイネップ","enName":"Otoineppu"},"12066":{"type":"C","elems":"01000000","lat":[44,36.5],"lon":[142,17.7],"alt":60,"kjName":"小車","knName":"オグルマ","enName":"Oguruma"},"12141":{"type":"C","elems":"11112100","lat":[44,28.8],"lon":[142,20.5],"alt":77,"kjName":"美深","knName":"ビフカ","enName":"Bifuka"},"12181":{"type":"C","elems":"11112110","lat":[44,22.2],"lon":[142,27.4],"alt":89,"kjName":"名寄","knName":"ナヨロ","enName":"Nayoro"},"12217":{"type":"C","elems":"01000000","lat":[44,17.7],"lon":[142,21.5],"alt":128,"kjName":"西風連","knName":"ニシフウレン","enName":"Nishi-Furen"},"12231":{"type":"C","elems":"11112100","lat":[44,17.7],"lon":[142,38.2],"alt":143,"kjName":"下川","knName":"シモカワ","enName":"Shimokawa"},"12256":{"type":"C","elems":"01000000","lat":[44,6.4],"lon":[142,21.2],"alt":138,"kjName":"剣淵","knName":"ケンブチ","enName":"Kenbuchi"},"12261":{"type":"C","elems":"11112000","lat":[44,11.2],"lon":[142,25],"alt":135,"kjName":"士別","knName":"シベツ","enName":"Shibetsu"},"12266":{"type":"C","elems":"11112000","lat":[44,7.1],"lon":[142,35.7],"alt":225,"kjName":"朝日","knName":"アサヒ","enName":"Asahi"},"12301":{"type":"C","elems":"11112100","lat":[44,1.7],"lon":[142,24.6],"alt":150,"kjName":"和寒","knName":"ワッサム","enName":"Wassamu"},"12386":{"type":"C","elems":"11112000","lat":[43,52.2],"lon":[142,15.6],"alt":140,"kjName":"江丹別","knName":"エタンベツ","enName":"Etanbetsu"},"12396":{"type":"C","elems":"11112000","lat":[43,52.2],"lon":[142,28.2],"alt":164,"kjName":"比布","knName":"ピップ","enName":"Pippu"},"12411":{"type":"C","elems":"11112100","lat":[43,50.8],"lon":[142,45.2],"alt":324,"kjName":"上川","knName":"カミカワ","enName":"Kamikawa"},"12442":{"type":"A","elems":"11111111","lat":[43,45.4],"lon":[142,22.3],"alt":120,"kjName":"旭川","knName":"アサヒ
カワ","enName":"Asahikawa"},"12451":{"type":"C","elems":"11112000","lat":[43,42.1],"lon":[142,30.5],"alt":215,"kjName":"東川","knName":"ヒガシカワ","enName":"Higashikawa"},"12457":{"type":"C","elems":"01000000","lat":[43,44.2],"lon":[142,38.3],"alt":289,"kjName":"瑞穂","knName":"ミズホ","enName":"Mizuho"},"12471":{"type":"C","elems":"01000100","lat":[43,45.2],"lon":[142,55.8],"alt":540,"kjName":"層雲峡
","knName":"ソウウンキョウ","enName":"Sounkyo"},"12501":{"type":"C","elems":"11110100","lat":[43,40.2],"lon":[142,26.8],"alt":211,"kjName":"東神楽","knName":"ヒガシカグラ：旭川空港","enName":"Higashi-Kagura"},"12512":{"type":"C","elems":"11112000","lat":[43,38.6],"lon":[142,34.9],"alt":310,"kjName":"志比内","knName":"シビナイ","enName":"Shibinai"},"12551":{"type":"C","elems":"11112100","lat":[43,35.3],"lon":[142,29.6],"alt":250,"kjName":"美瑛","knName":"ビエイ","enName":"Biei"},"12596":{"type":"C","elems":"11112000","lat":[43,27.3],
```
とします。

プログラムに読ませる、容量を節約したいJSONファイルを使いたい場合はこれでよさげですね。