[英]Change entry in a JSON list that matches a condition without discarding rest of document
I am trying to open a file, look through the file and change a value based on the value and pass this either to a file or var. 我正在尝试打开一个文件,浏览文件并根据值更改值,并将其传递给文件或var。 Below is an example of the JSON
下面是JSON的示例
{
"Par": [
{
"Key": "12345L",
"Value": "https://100.100.100.100:100",
"UseLastValue": true
},
{
"Key": "12345S",
"Value": "VAL2CHANGE",
"UseLastValue": true
},
{
"Key": "12345T",
"Value": "HAPPY-HELLO",
"UseLastValue": true
}
],
"CANCOPY": false,
"LOGFILE": ["HELPLOG"]
}
i have been using jq and i have been successful in isolating the object group and change the value. 我一直在使用jq,我已成功隔离对象组并更改了值。
cat jsonfile,json | jq '.Par | map(select(.Value=="VAL2CHANGE")) | .[] | .Value="VALHASBEENCHANGED"'
This gives 这给了
{
"Key": "12345S",
"Value": "VALHASBEENCHANGED",
"UseLastValue": true
}
What id like to achieve is to retain the full JSON output with the changed value id想要实现的是使用更改的值保留完整的JSON输出
{
"Par": [
{
"Key": "12345L",
"Value": "https://100.100.100.100:100",
"UseLastValue": true
},
{
"Key": "12345S",
"Value": "VALHASBEENCHANGED",
"UseLastValue": true
},
{
"Key": "12345T",
"Value": "HAPPY-HELLO",
"UseLastValue": true
}
],
"CANCOPY": false,
"LOGFILE": ["HELPLOG"]
}
IE IE
jq '.Par | map(select(.Value=="VAL2CHANGE")) | .[] | .Value="VALHASBEENCHANGED"' (NOW PUT IT BACK IN FILE)
OR 要么
open file, look in file, file value to be changed and change this and output this to a file or to screen 打开文件,查看文件,要更改的文件值并更改它并将其输出到文件或屏幕
To add, the json file will only contain the value im looking for once as im creating this. 要添加,json文件将仅包含我正在寻找一次的值,因为即时创建它。 If any other values need changing i will name differently.
如果任何其他值需要更改,我的名字会有所不同。
jq --arg match "VAL2CHANGE" \
--arg replace "VALHASBEENCHANGED" \
'.Par |= map(if .Value == $match then (.Value=$replace) else . end)' \
<in.json
To more comprehensively replace a string anywhere it may be in a nested data structure, you can use the walk
function -- which will be in the standard library in jq 1.6, but can be manually pulled in in 1.5: 要更全面地替换嵌套数据结构中任何位置的字符串,可以使用
walk
函数 - 它将位于jq 1.6中的标准库中,但可以在1.5中手动拉入:
jq --arg match "VAL2CHANGE" \
--arg replace "VALHASBEENCHANGED" '
# taken from jq 1.6; will not be needed here after that version is released.
# Apply f to composite entities recursively, and to atoms
def walk(f):
. as $in
| if type == "object" then
reduce keys_unsorted[] as $key
( {}; . + { ($key): ($in[$key] | walk(f)) } ) | f
elif type == "array" then map( walk(f) ) | f
else f
end;
walk(if . == $match then $replace else . end)' <in.json
If you're just replacing based on the values, you could stream the file and replace the values as you rebuild the result. 如果您只是根据值进行替换,则可以在重建结果时流式传输文件并替换值。
$ jq --arg change 'VAL2CHANGE' --arg value 'VALHASBEENCHANGED' -n --stream '
fromstream(inputs | if length == 2 and .[1] == $change then .[1] = $value else . end)
' input.json
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.