3

pretty simple:

What I have: '["a","b","c"]'

What I want:

{
  "1":"a",
  "2":"b",
  "3":"c"
}

What I am working with.

echo '["a","b","c"]' | jq '. | map({(index(.)) : (.)})'

What I am getting (it seems to be operating on successively smaller arrays):

 echo '["a","b","c"]' | jq '. | map(index((.)))'
[
  0,
  0,
  0
]

1 Answer 1

6

A functional solution:

[to_entries[] | {(.key+1|tostring): .value}] | add

Or, more succinctly:

with_entries(.key |= (1+.|tostring))

Or, more prosaically:

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

1 Comment

Prosaically: When you do something prosaically, you do it in an ordinary, straightforward way. Succinctly: A way that expresses what needs to be said clearly and without unnecessary words. (For the non-english speakers ;) )

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.