2017-10-13 82 views
1

是否可以對json文檔的特定數組進行排序,然後輸出帶有排序內容的整個文檔,否則不會被觸動?輸出帶有排序內容的整個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'進行排序的工作,但我想回到整個文檔格式化的上下文中。

回答

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 
+0

太棒了!這確實有效,你是否打算分解它並解釋這實際上是如何完成的? – TryTryAgain

+1

@TryTryAgain,當然,請參閱我的解釋 – RomanPerekhrest

+2

幾條評論:1.您不必將其限制爲第一條語句,您可以將其保留爲'.Statement []。Principal ...',2. the當不需要排序時最初的'。',它可能只是'排序'。 '.Statement []。Principal.AWS | = sort' –