【发布时间】:2020-01-02 23:42:47
【问题描述】:
我正在尝试解析 JSON,但在迭代数组时遇到了问题。
[
{
"severity": "low",
"name": "AWS IAM policy attached to users",
"rule": "$.resource[*].aws_iam_policy_attachment[*].*[*].users exists and $.resource[*].aws_iam_policy_attachment[*].*[*].users[*] is not empty",
"files": [
"main.tf"
],
"id": "1903f355-b68f-4d9c-84dd-c46abe4f8673"
},
{
"severity": "medium",
"name": "AWS VPC allows unauthorized peering",
"rule": "$.resource[*].aws_vpc_peering_connection[*].*[*].peer_vpc_id does not equal $.resource[*].aws_vpc_peering_connection[*].*[*].vpc_id",
"files": [
"vpcpeering.tf",
"main.tf"
],
"id": "59356130-d856-470d-a08e-b2a0ba2a4ac7"
}
]
. += [{"severity": "Severity","name": "Name","files": "Files","id": "0"}, {"severity": "--------","name": "------------------------------------","files": "--------","id": "01"}] | sort_by(.id) | .[] | ["| " + .severity, "| " + .name, "| " + (.files | join (",")) , "| " ]
jq: error (at <stdin>:131): Cannot iterate over string ("Files")
exit status 5
https://jqplay.org/s/KzNXa7NqKq
我正在尝试以表格格式打印结果。
Severity. Name. Files
low AWS IAM policy attached to users. main.tf
low AWS VPC allows unauthorized peering. vpcpeering.tf, main.tf
【问题讨论】:
-
您添加的对象的
files属性不是数组,而是字符串。你“不能遍历字符串”,把它们放在数组中。 -
请遵循minimal reproducible example 准则。你能简化输入(同时保持有效的 JSON)吗?预期的输出是什么?
标签: jq