I have the following JQ problem that I can't solve. Given JSON:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::111122223333:root"
},
"Action": "sts:AssumeRole"
}
]
}
I need to take the value .Principal.AWS and save that in a variable $orig or something, and then readd that variable as an element in an array that will become the new document.
This almost works:
'.Statement[0]
| .Principal.AWS as $orig
| .Principal.AWS = [$orig] + ["foo", "bar"]'
Doing that, however, I lose the Version and Statement level. Meanwhile, doing this nearly does what I want:
'(.Statement[] | .Principal.AWS) = ["foo", "bar"]'
So I expected this to also work but it doesn't:
'(.Statement[] | .Principal.AWS as $orig | .Principal.AWS) = [$orig, "foo", "bar"]'
Then I get an invalid path expression error.
How can I correct this syntax? The expected result is:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": ["arn:aws:iam::111122223333:root", "foo", "bar"]
},
"Action": "sts:AssumeRole"
}
]
}