输出带有已排序内容的整个JSON文档

时间:2017-10-13 12:54:43

标签: json sorting jq jmespath

是否可以对json文档的特定数组进行排序,并输出整个文档,其中包含未经修改的已排序内容?

例如,如果我有这样的事情:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "BLAHBLAHBLAH",
      "Effect": "Allow",
      "Principal": {
        "AWS": [
          "arn:aws:iam::346654243625:root",
          "arn:aws:iam::984895836564:root",
          "arn:aws:iam::636877599363:root",
          "arn:aws:iam::577836285792:root",
          "arn:aws:iam::836666644595:root",
          "arn:aws:iam::652379234777:root",
          "arn:aws:iam::339659563489:root",
          "arn:aws:iam::293576423935:root",
          "arn:aws:iam::682354689262:root",
          "arn:aws:iam::349855857558:root",
          "arn:aws:iam::398259495958:root",
          "arn:aws:iam::735277384553:root"
        ]
      },
      "Action": "s3:*",
      "Resource": [
        "arn:aws:s3:::blah-blah-blah",
        "arn:aws:s3:::blah-blah-blah/*"
      ]
    }
  ]
}

我想回复一下:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "BLAHBLAHBLAH",
      "Effect": "Allow",
      "Principal": {
        "AWS": [
          "arn:aws:iam::293576423935:root",
          "arn:aws:iam::339659563489:root",
          "arn:aws:iam::346654243625:root",
          "arn:aws:iam::349855857558:root",
          "arn:aws:iam::398259495958:root",
          "arn:aws:iam::577836285792:root",
          "arn:aws:iam::636877599363:root",
          "arn:aws:iam::652379234777:root",
          "arn:aws:iam::682354689262:root",
          "arn:aws:iam::735277384553:root",
          "arn:aws:iam::836666644595:root",
          "arn:aws:iam::984895836564:root"
        ]
      },
      "Action": "s3:*",
      "Resource": [
        "arn:aws:s3:::blah-blah-blah",
        "arn:aws:s3:::blah-blah-blah/*"
      ]
    }
  ]
}

我已经看到使用jqJMESPath排序示例,但只看到它们输出要排序的部分,而不是输出已排序部分的整个文档。寻找有关此事的指导,并对任何有效的解决方案感到满意。

这是我用来排序有效的数组| jq '.Statement[].Principal.AWS|sort',但是我想回到整个文档的上下文格式。

1 个答案:

答案 0 :(得分:3)

对于 jq 处理器来说,这是一个不错的选择:

jq '.Statement[0].Principal.AWS |= sort' file
  • .Statement[0].Principal.AWS - 所选项目

  • |= - 更新声明/操作(更新所选项目)

输出:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "BLAHBLAHBLAH",
      "Effect": "Allow",
      "Principal": {
        "AWS": [
          "arn:aws:iam::293576423935:root",
          "arn:aws:iam::339659563489:root",
          "arn:aws:iam::346654243625:root",
          "arn:aws:iam::349855857558:root",
          "arn:aws:iam::398259495958:root",
          "arn:aws:iam::577836285792:root",
          "arn:aws:iam::636877599363:root",
          "arn:aws:iam::652379234777:root",
          "arn:aws:iam::682354689262:root",
          "arn:aws:iam::735277384553:root",
          "arn:aws:iam::836666644595:root",
          "arn:aws:iam::984895836564:root"
        ]
      },
      "Action": "s3:*",
      "Resource": [
        "arn:aws:s3:::blah-blah-blah",
        "arn:aws:s3:::blah-blah-blah/*"
      ]
    }
  ]
}

要对多个数组进行排序,请使用:

jq '.Statement[].Principal.AWS |= sort' file