【问题标题】:jq: error (at <stdin>:131): Cannot iterate over string ("Files")jq:错误(在 <stdin>:131):无法迭代字符串(“文件”)
【发布时间】: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


【解决方案1】:

由于 .files 数组中的项目数是可变的,以下将重点介绍生成 TSV(制表符分隔值)。您可以轻松地根据您对具有可变列数的表的要求调整此解决方案。

首先,忽略标题,注意以下过滤器与 -r 命令行选项结合使用时,会产生如下所示的输出:

map({id, severity, name, files})
| sort_by(.id)[]
| [.severity, .name, .files[]]
| @tsv

输出

low AWS IAM policy attached to users    main.tf
medium  AWS S3 object versioning is disabled    cloudtrail.tf.json  cloudtrail.tf
medium  AWS VPC NACL allows traffic from blocked ports  SG.tf
medium  AWS security group allow egress traffic from blocked ports - 21,22,135,137-139,445,69   securitygroup22.tf
medium  AWS Access logging not enabled on S3 buckets    cloudtrail.tf.json  cloudtrail.tf
medium  AWS VPC allows unauthorized peering vpcpeering.tf
medium  AWS IAM password policy does not have a minimum of 14 characters    iampassword.tf
medium  AWS security group allows traffic from blocked ports    securitygroup22.tf
high    AWS Security Groups allow internet traffic to SSH port (22) securitygroup22.tf
medium  AWS EC2 instance have SSH port open to internet securitygroup22.tf
medium  AWS IAM password policy allows password reuse   iampassword.tf
medium  AWS VPC NACL allow egress traffic from blocked ports    SG.tf
high    AWS Security Groups with Inbound rule overly permissive to All Traffic  securitygroup22.tf

标题

处理标题行的模块化方法是定义headers,例如沿着这些思路:

def headers:
  ["Severity","Name","Files"],
  ["--------","----","-----"] ;

有了这个def,我们基本上只是将headers添加到之前的jq程序中:

headers,
(map({id, severity, name, files})
 | sort_by(.id)[]
 | [.severity, .name, .files[]])
| @tsv

为 .files 使用不同的“加入”字符

而不是行: [.severity, .name, .files[]]

您可能希望对文件使用不同的“加入”字符,例如

[.severity, .name, (.files|join(";"))]

调味。

【讨论】:

  • 如何在输出中添加标题?
  • 发布的答案中已经涵盖了一些内容,因此您需要更具体地了解您的要求。
  • 抱歉错过了标题部分。现在就试试。
  • [更新}:它就像一个魅力。非常感谢!!
猜你喜欢
  • 2021-04-27
  • 2020-01-06
  • 2019-07-14
  • 1970-01-01
  • 2019-11-02
  • 1970-01-01
  • 1970-01-01
  • 2015-03-28
  • 2019-09-14
相关资源
最近更新 更多