Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

json

jq

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
like image 257
Tyler Avatar asked Dec 03 '25 09:12

Tyler


1 Answers

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)
like image 59
peak Avatar answered Dec 05 '25 02:12

peak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!