1

I'm really struggling with this simple task. I'm trying to convert this:

[
    "arn:aws:iam::123456789012:role/masters.a.eu.somewhere.com",
    "arn:aws:iam::123456789012:role/masters.b.eu.somewhere.com",
    "arn:aws:iam::123456789012:role/masters.c.eu.somewhere.com"
]

To something like this:

{
    "0": "arn:aws:iam::123456789012:role/masters.a.eu.somewhere.com",
    "1": "arn:aws:iam::123456789012:role/masters.b.eu.somewhere.com",
    "2": "arn:aws:iam::123456789012:role/masters.c.eu.somewhere.com"
}

Using just JQ. I've tried using 'to_entries' but I'm not getting the desired output. Any suggestions appreciated.

2
  • Where are a, b and c coming from? Just a for the first element, b for the second etc., or extracted from the URL somehow? Commented Feb 15, 2019 at 20:43
  • Just a for the first, b for the second, c for the third. I've ammended my example with numbered elements as that was as close as I could get with to_entries. Just not the desired format. Commented Feb 15, 2019 at 20:46

1 Answer 1

2

Admittedly this is not obvious, but a very short solution is:

with_entries(.key |= tostring)

A more pedestrian but still perfectly respectable solution would be:

. as $in
| reduce range(0;length) as $i ({}; . + {($i|tostring): $in[$i]})
Sign up to request clarification or add additional context in comments.

2 Comments

A maybe slightly more scrutable version (that doesn't depend on the "arrays have number keys, objects have string keys" magic) would be to_entries | map({ (.key | tostring): .value }) | add
Very much appreciated. As you say not obvious but logical now I see it. Upvoted the additional suggestion from hobbs as I can see that being more useful to me down the line with regards additional transformations.

Your Answer

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