1

I am very new to jq, I am trying to parse an output json I get from command -

aws cloudformation list-stacks --stack-status-filter CREATE_COMPLETE

to get a new output where values of the key matches to certain pattern.

for example this is my json output from previous command -

{
  "StackSummaries": [
    {
      "StackId": "arn:aws:213dqwqwdqwdqwdq",
      "StackName": "monkeyman",
      "CreationTime": "2017-06-06T20:52:59.728Z",
      "StackStatus": "CREATE_COMPLETE",
      "TemplateDescription": "Liaison API ELB cloud formation script"
    },
    {
      "StackId": "arn:aws:csdfsdfcsdfsdfsdfsdfsdfgdfgfdg",
      "StackName": "monkeyman2",
      "CreationTime": "2017-06-06T20:51:55.191Z",
      "StackStatus": "CREATE_COMPLETE",
      "TemplateDescription": "yoohooo instance"
    },
    {
      "StackId": "arn:aws:sdffgds444fsdfsdfgdfgfdg",
      "StackName": "starfish2",
      "CreationTime": "2017-06-06T20:51:55.191Z",
      "StackStatus": "CREATE_COMPLETE",
      "TemplateDescription": "helloworld instance"
    },
    {
      "StackId": "arn:aws:csdfsdfcsdfsdfsdfsdfsdfgdfgfdg",
      "StackName": "bulldog4",
      "CreationTime": "2017-06-06T20:51:55.191Z",
      "StackStatus": "CREATE_COMPLETE",
      "TemplateDescription": "night night instance"
    },
    {
      "StackId": "arn:aws:yhyhyhyhyhysdfgdfgfdg",
      "StackName": "carrotman",
      "CreationTime": "2017-06-06T20:51:55.191Z",
      "StackStatus": "CREATE_COMPLETE",
      "TemplateDescription": "surprise mo instance"
    }

  ]

}

Here I need to create a new output json by piping the first output to jq where StackName startsWith monkeyman and bulldog and which should look like -

{
  "StackSummaries": [
    {
      "StackId": "arn:aws:213dqwqwdqwdqwdq",
      "StackName": "monkeyman",
      "CreationTime": "2017-06-06T20:52:59.728Z",
      "StackStatus": "CREATE_COMPLETE",
      "TemplateDescription": "Liaison API ELB cloud formation script"
    },
    {
      "StackId": "arn:aws:csdfsdfcsdfsdfsdfsdfsdfgdfgfdg",
      "StackName": "monkeyman2",
      "CreationTime": "2017-06-06T20:51:55.191Z",
      "StackStatus": "CREATE_COMPLETE",
      "TemplateDescription": "yoohooo instance"
    },
    {
      "StackId": "arn:aws:csdfsdfcsdfsdfsdfsdfsdfgdfgfdg",
      "StackName": "bulldog4",
      "CreationTime": "2017-06-06T20:51:55.191Z",
      "StackStatus": "CREATE_COMPLETE",
      "TemplateDescription": "night night instance"
    }

  ]

}

I have tried a lot, I was able to do it somehow using basic unix split and regex commands but something tells me it would be easier and less cumbersome to do directly with jq.

1

1 Answer 1

3

It is easy with jq

jq '.[] |= map(select(.StackName | startswith("monkeyman"), startswith("bulldog")))'

In a nutshell, map(x) applies x to every object that is fed to it. select(y) selects the objects that meet the condition y. In this case, condition y is that there is a key called StackName whose value starts with either "monkeyman" or "bulldog".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.