LoginSignup
6
0

More than 3 years have passed since last update.

JSONデータをエンコード・デコードしたい

Last updated at Posted at 2020-07-22

jqコマンドをインストールする

Macの場合

brew install jq

JSONデータが入ったファイルを整形する(エンコード)

sample.jsonファイル:JSONデータが入っているファイル

jq . sample.json > tmp && mv tmp sample.json 

vimで編集

vim sample.json

戻す(デコード)

jq -c . sample.json > tmp && mv tmp sample.json

UTF-8の文字列が入ったJSONデータは自動的に日本語になる
そのままUTF-8で受け取りたい場合は 「-a」 をつけてASCIIにエンコードする

jq -ac . sample.json > tmp && mv tmp sample.json

以下の記事を参考にしました
https://takuya-1st.hatenablog.jp/entry/2019/07/09/174427

整形したJSONデータのみを確認したい場合

echoを使う

$ echo '{"items":[{"item_id":1,"name":"すてきな雑貨","price":2500},{"item_id":2,"name":"格好いい置物","price":4500}]}' \
| jq .
{
  "items": [
    {
      "item_id": 1,
      "name": "すてきな雑貨",
      "price": 2500
    },
    {
      "item_id": 2,
      "name": "格好いい置物",
      "price": 4500
    }
  ]
}

以下の記事を参考にしました
https://qiita.com/takeshinoda@github/items/2dec7a72930ec1f658af

6
0
0

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
6
0