Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any JQ built-in function returning index of element in JSON array?

Tags:

json

arrays

jq

I have created a csv output from an input JSON file.

Since some JSON arrays do not have their own id's, I need to add a unique id in my csv output that will be based on index of current element in its JSON array.

Is there any built-in JQ function returning the element index?

like image 893
Bob Neurone Avatar asked Sep 10 '15 10:09

Bob Neurone


1 Answers

to_entries should work perfectly.

jq -n '["a","b","c"] | to_entries'

will produce

[{"key":0,"value":"a"},{"key":1,"value":"b"},{"key":2,"value":"c"}]
like image 130
Joseph K. Strauss Avatar answered Oct 22 '22 14:10

Joseph K. Strauss