1

I have below json file :

{
"EventId": "60a0490c",
"Resources": [
    {
        "ResourceType": "AWS::STS::AssumedRole", 
        "ResourceName": "AutoScaling"
    }, 
    {
        "ResourceType": "AWS::IAM::Role", 
        "ResourceName": "arn:aws:iam:autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling"
    }
    ]
}

I want to concatenate the key-value pairs from Resources element, and output on single line as:

60a0490c,AutoScaling=AWS::STS::AssumedRole#AWS::IAM::Role=arn:aws:iam:autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling

I tried it as :

cat file.json | jq '.EventId + "," + (.Resources[] | join("="))' -r

It gives me output as :

60a0490c,AutoScaling=AWS::STS::AssumedRole
60a0490c,AutoScaling=AWS::IAM::Role=arn:aws:iam:autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling

It creates new entry for each key-value pair, I want it to be on single line with different delimeter (#)

Thanks in advance.

2
  • Are you sure the command that you use give the output shown by you? On my mac output is different: 60a0490c,AWS::STS::AssumedRole=AutoScaling 60a0490c,AWS::IAM::Role=arn:aws:iam:autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling Commented Sep 18, 2018 at 11:08
  • Hi Ashutosh, I tried my above command on both ubuntu and mac, same output. $ jq --version jq-1.5-1-a5b5cbe Commented Sep 18, 2018 at 11:47

1 Answer 1

6

You can try this:

jq ' .EventId + "," + ([(.Resources[] | join("="))] | join("#"))' -r file
60a0490c,AWS::STS::AssumedRole=AutoScaling#AWS::IAM::Role=arn:aws:iam:autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling

To use the second join(), just enclose the result in an array [...].

Note that the first key/value is not swapped as shown in your example (i.e. Autoscaling is the value and not the key).

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

1 Comment

Yes, this solved my problem, Thanks oliv for quick response.

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.