0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Mac】jqコマンドの使い方

Posted at

jqはJSONデータを処理するためのコマンドラインツールです。

インストール

以下のコマンドでHomebrewを使用してインストールできます。

brew install jq

使い方

特定のJSONファイルを色付きかつフォーマットされた状態で表示します。

jq '.' /path/to/file.json

特定のスクリプトを使用してJSONデータを処理するには、次のようにします。

cat path/to/file.json | jq --from-file path/to/script.jq

特定の引数を渡してJSONデータを操作するには、以下のコマンドを使用します。

cat path/to/file.json | jq --arg "name1" "value1" --arg "name2" "value2" ... '. + $ARGS.named'

複数のJSONファイルから新しいJSONオブジェクトを作成するには、次のようにします。

cat path/to/multiple_json_file_*.json | jq '{newKey1: .key1, newKey2: .key2.nestedKey, ...}'

特定のインデックスの配列アイテムを表示するには、以下のコマンドを使用します。

cat path/to/file.json | jq '.[index1], .[index2], ...'

JSONファイル内のすべての配列またはオブジェクトの値を表示するには、次のようにします。

cat path/to/file.json | jq '.[]'

特定の条件を満たすオブジェクトをフィルタリングして表示するには、以下のコマンドを使用します。

cat path/to/file.json | jq '.[] | select((.key1=="value1") and .key2=="value2")'

特定のキーをJSONオブジェクトに追加または削除するには、次のようにします。

cat path/to/file.json | jq '. + {"key1": "value1", "key2": "value2", ...}'  # 追加
cat path/to/file.json | jq '. - {"key1": "value1", "key2": "value2", ...}'  # 削除

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?