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?

jq コマンドで json の特定のフィールドを削る・抽出する

Last updated at Posted at 2024-08-18

json例

	             {
                     "response": {
                       "num": 2,
                       "object": [
                         {"id": "a1", "name": "名前1", "price": 1000, "updated_at": "2024-06-26"},
                         {"id": "b2", "name": "名前2", "price": 2000, "updated_at": "2024-06-30"}
                       ]
                     }
                  }

抽出 

// object内の配列を抽出
echo '{"response": {"num": 2,"object": [{"id": "a1", "name": "名前1", "price": 1000, "updated_at": "2024-06-26"},{"id": "b2", "name": "名前2", "price": 2000, "updated_at": "2024-06-30"}]}}' | jq '.response.object[].id'

"a1"
"b2"

// numとobjectを抽出
echo '{"response": {"num": 2,"object": [{"id": "a1", "name": "名前1", "price": 1000, "updated_at": "2024-06-26"},{"id": "b2", "name": "名前2", "price": 2000, "updated_at": "2024-06-30"}]}}' | jq '.response.object[],.response.num'

{
  "id": "a1",
  "name": "名前1",
  "price": 1000,
  "updated_at": "2024-06-26"
}
{
  "id": "b2",
  "name": "名前2",
  "price": 2000,
  "updated_at": "2024-06-30"
}
2

削除 del

使い所:新旧のレスポンスの差分を見るときにいらないところを削除する
    update_atだけ異なるので削除したい場合

//update_atだけ削除
echo '{"response": {"num": 2,"object": [{"id": "a1", "name": "名前1", "price": 1000, "updated_at": "2024-06-26"},{"id": "b2", "name": "名前2", "price": 2000, "updated_at": "2024-06-30"}]}}' | jq 'del(.response.object[].updated_at)'

{
  "response": {
    "num": 2,
    "object": [
      {
        "id": "a1",
        "name": "名前1",
        "price": 1000
      },
      {
        "id": "b2",
        "name": "名前2",
        "price": 2000
      }
    ]
  }
}
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?