2017-10-13 7 views
1

Est-il possible de trier un tableau particulier d'un document json et de sortir le document entier avec le contenu trié sinon intact?Sortie d'un document JSON entier avec du contenu trié

Par exemple, si j'ai quelque chose comme ceci:

{ 
    "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/*" 
     ] 
    } 
    ] 
} 

Je voudrais revenir ceci:

{ 
    "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/*" 
     ] 
    } 
    ] 
} 

J'ai vu des exemples de tri en utilisant jq et JMESPath mais seulement jamais vu ils sortent la section à trier et non le document entier avec la section triée. Vous cherchez des conseils en la matière et serait heureux avec toute solution de travail.

C'est ce que j'ai utilisé pour trier le tableau | jq '.Statement[].Principal.AWS|sort' qui fonctionne, mais je voudrais revenir dans le contexte de l'ensemble du document tel qu'il était.

Répondre

3

Il est un bon travail pour JQ processeur:

jq '.Statement[0].Principal.AWS |= sort' file 
  • .Statement[0].Principal.AWS - élément sélectionné

  • |=-mise à jour déclaration/action (mettre à jour l'élément sélectionné)


La sortie:

{ 
    "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/*" 
     ] 
    } 
    ] 
} 

Pour trier plusieurs tableaux utilisent:

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

Fantastique! Cela fonctionne effectivement, vous souciez-vous de le décomposer et d'expliquer comment cela accomplit réellement cela? – TryTryAgain

+1

@TryTryAgain, bien sûr, voir mon explication – RomanPerekhrest

+2

Couple de commentaires: 1. vous ne devez pas le restreindre à la première déclaration, vous pouvez le laisser comme '.Statement []. Principal ...', 2. le initial '.' quand le tri est inutile, il pourrait simplement être' sort'. '.Statement []. Principal.AWS | = sort' –