Passing JSON Objects to to_entries
If a
JSON object is passed to
to_entries
, it creates an array of JSON object each of which has the two keys
key
and
value
.
$ echo '{
"txt": "Hello world",
"num": 42
}' | jq to_entries
[
{
"key": "txt",
"value": "Hello world"
},
{
"key": "num",
"value": 42
}
]
If the input is an array of objects, it should probably be passed to the
.[]
filter:
$ echo '
[
{
"txt": "Hello world",
"num": 42
},
{
"key-one": "val 1",
"key-two": "val 2"
}
]' | jq ' .[] | to_entries'
[
{
"key": "txt",
"value": "Hello world"
},
{
"key": "num",
"value": 42
}
]
[
{
"key": "key-one",
"value": "val 1"
},
{
"key": "key-two",
"value": "val 2"
}
]
Passing an array to to_entries
If an
array is passed to
to_entries
, the values for the
key
correspond to the indices in the array:
$ echo '[ "foo", "bar", "baz" ]' | jq to_entries
[
{
"key": 0,
"value": "foo"
},
{
"key": 1,
"value": "bar"
},
{
"key": 2,
"value": "baz"
}
]