How to fetch first element from a stream of JSON objects?

Viewed 138

Example

{
  "name": "bar",
  "thing": true,
  "new_key": 0
}
{
  "name": "baz",
  "thing": false,
  "new_key": 0
}

If I run .name I get output as

bar
baz

I want only bar

1 Answers

You can use first/1 in conjunction with -n/--null-input flag.

jq -n 'first(inputs.name)'

Online demo

Related