json - 使用 JQ 更新 json 对象的部分值
问题描述
在 JSON 对象中有一个值,其中有一个需要使用 JQ 更新的变量,下面是我的儿子文件和我用来更新json 文件中的accountid的 jq 命令
{"bucketname":"test/$accountid/123","objectname":"test/$accountid/123","targetlocation":"$TARGET_LOCATION"}
JQ 命令
jq 'map(.$accountid = "123")' myjson.json
错误
jq: error (at myjson.json:1): Cannot index string with string "$accountid"
我在这里错过了什么吗?
解决方案
从评论看来,您应该使用map_values
,也许像sub
. 因此考虑这个例子:
echo '{"bucketname":"test/$accountid/123","objectname":"test/$accountid/123","targetlocation":"$TARGET_LOCATION"}' |
jq 'map_values( sub("[$]accountid";"123") )'
产生:
{
"bucketname": "test/123/123",
"objectname": "test/123/123",
"targetlocation": "$TARGET_LOCATION"
}
锐化
您可能想提高替代标准,例如
sub("[$]accountid(?<tail>(/|$))"; "123\(.tail)" )
推荐阅读
- java - 在 Java 中创建注册表/数据库列表
- python - 加载所有相关对象而不使用命名表
- sql-server - SQL Server:自联接查询;仅选择与名字匹配的记录,而没有 where 语句
- ios - 在 Swift 中隐藏 TabbedBarController 上的后退按钮
- sql - 如何翻译 SQL 语句以在 sqlx API 调用中使用?
- logging - NLog Core 2.0 自动记录
- c# - 如果 linq 查询中没有数据,我如何默认返回 0 作为十进制值?
- android - 为什么在 Volley 响应结果循环下 arrayList 总是为空?
- javascript - 在一行表上显示按钮
- ios - 异步图像设置为具有动态高度错误的单元格