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
}
]
}
}