特定の条件で更新する必要があるjsonファイルがあります。
サンプル json
{
"Actions" : [
{
"value" : "1",
"properties" : {
"name" : "abc",
"age" : "2",
"other ": "test1"
}
},
{
"value" : "2",
"properties" : {
"name" : "def",
"age" : "3",
"other" : "test2"
}
}
]
}
以下に示すように、Jqを使用して値と更新を一致させるスクリプトを作成しています
cat sample.json | jq '.Actions[] | select (.properties.age == "3") .properties.other = "no-test"'
出力 (端末に出力)
{
"value": "1",
"properties": {
"name": "abc",
"age": "2",
"other ": "test1"
}
}
{
"value": "2",
"properties": {
"name": "def",
"age": "3",
"other": "no-test"
}
}
このコマンドは必要な変更を行いますが、json 全体をターミナルに出力し、ファイル自体は変更しません。
jq でファイルを直接変更するオプションがあるかどうかをお知らせください (sed -i と同様)。