Tyler
Tyler

Reputation: 22136

jq from_entries function works with 'key' but not 'name'

According to the documentation the from_entries function works with objects that look like {"key": something, "value": something}, or {"name": something, "value": something}. However, the second form is not working for me. The example from the docs works:

$ echo '[{"key":"a", "value":1}, {"key":"b", "value":2}]' | jq from_entries
{
  "a": 1,
  "b": 2
}

but the same example, except using "name" instead of "key" does not:

$ echo '[{"name":"a", "value":1}, {"name":"b", "value":2}]' | jq from_entries
jq: error (at <stdin>:1): Cannot use null (null) as object key

Why is this? My jq version is

$ jq --version
jq-1.5-1-a5b5cbe

Upvotes: 3

Views: 4951

Answers (1)

peak
peak

Reputation: 116967

Unfortunately, jq 1.5 does not include the enhancement regarding "name".

(Please note that the jq 1.5 documentation correctly advertises "Name" as an alternative to "key", but not "name".)

You will either have to upgrade your jq, or use a workaround, such as:

map(with_entries(if .key == "name" then .key = "key" else . end))
| from_entries

or more generically:

def from_entries(key; value):
  map( {(key): value} ) | add | . // {} ;

from_entries(.name; .value)

Upvotes: 6

Related Questions