json - 替换 JSON 文档中的变量
问题描述
我正在为以下输入 JSON 文档使用此示例cat input.json | jq --arg REGION "ap-south-1" --arg ACCOUNTID "46311000000" --arg QNAME "orders"
,使用. 但是,输出没有显示任何转换。
你能告诉我如何申请更换吗?
input.json
{
"Version": "2012-10-17",
"Id": "policy-15122020",
"Statement": [
{
"Sid": "1",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": [
"sqs:ReceiveMessage",
"sqs:SendMessage"
],
"Resource": "arn:aws:sqs:$REGION:$ACCOUNTID:$QNAME",
"Condition": {
"StringEquals": {
"aws:SourceAccount": "$ACCOUNTID"
}
}
}
]
}
解决方案
With JQ 1.6, you can use $ARGS builtin variable in conjunction with gsub and a named capturing group to achieve this.
jq --arg REGION "ap-south-1" \
--arg ACCOUNTID "46311000000" \
--arg QNAME "orders" '
walk(
if type == "string"
then gsub("\\$(?<name>\\w+)"; $ARGS.named[.name])
else . end
)' input.json
推荐阅读
- python - 如何使我的代码满足此要求
- node.js - Docker Debian .NET Core 镜像错误
- godot - func _physics_process(delta)、move_and_slide()、Vector2() 和 gdscript 中的作用是什么?
- laravel - Laravel 调度程序通过桃子付款向用户收取定期付款
- python - sqlite3.OperationalError:没有这样的列:| 不和谐.py
- java - Java 内存模型:关于顺序一致性的 JLS 声明似乎不正确
- python - Python pandas 数据透视表不返回所有列
- git - 推送到 git repo 时,我不断收到此警告
- azureservicebus - 如何在 Spring Boot Java 中将失败消息推送到 Azure 服务总线死信队列?
- ruby-on-rails - 为什么我无法从我的 React 前端表单上传和存储文件/图像到我的 Rails API 后端,安装了 Active Storage?