LoginSignup
11

More than 5 years have passed since last update.

JSONのrawデータをjqコマンドで整形してファイル出力する

Posted at

今回は簡単なコマンドの共有です。

準備

jq コマンドをダウンロードする

下記のサイトからダウンロードして下さい。
https://stedolan.github.io/jq/

cat コマンドが使いたい

Windows をお使いの場合は cat コマンドを入手してみて下さい。
私は GitBash を入れました → https://git-for-windows.github.io/

rawデータのJSONを準備

例えばこんなファイル(※文字コードはUTF8)
#元データはこちら → http://qiita.com/niiyz/items/3f522c0e5a32de916ec6

raw.json
{ "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": { "pref": "富山県", "city1": "下新川郡", "city2": "朝日町" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 136.111111, 36.111111 ], [ 136.222222, 36.222222 ] ] ] } }, { "type": "Feature", "properties": { "pref": "富山県", "city1": "氷見市", "city2": "" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 136.333333, 36.333333 ], [ 136.444444, 36.444444 ] ] ] } }, { "type": "Feature", "properties": { "pref": "富山県", "city1": "高岡市", "city2": "" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 136.555555, 36.555555 ], [ 138.666666, 36.666666 ] ] ] } } ]}

jqコマンド実行!

cmd
cat raw.json | jq . > pretty.json

実行結果

pretty.json
{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "pref": "富山県",
        "city1": "下新川郡",
        "city2": "朝日町"
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              136.111111,
              36.111111
            ],
            [
              136.222222,
              36.222222
            ]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "pref": "富山県",
        "city1": "氷見市",
        "city2": ""
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              136.333333,
              36.333333
            ],
            [
              136.444444,
              36.444444
            ]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "pref": "富山県",
        "city1": "高岡市",
        "city2": ""
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              136.555555,
              36.555555
            ],
            [
              138.666666,
              36.666666
            ]
          ]
        ]
      }
    }
  ]
}

このような形に整形されました♪

誰かにJSONデータのサンプルを渡す際には、rawデータを渡すより見やすいので、
こういったコマンドで変換してから渡してあげると良さそうですね^^

#サーバー側でやってくれるサービスもありますが、機密情報だと使いづらいですしね。。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11