Split multiple input JSONs with jq

2019-07-14 02:46发布

Given a JSON line

{"a":0,"b":{"c":"C"}}{"x":33}{"asd":889}

of 3 independent JSON objects.

And need to handle then one by one. It would be nice to have something like

echo "$json" | jq --first-one

Expected output:

{"a":0,"b":{"c":"C"}}

I found the only command which can remove first object and output others. inputs

echo '{"a":0,"b":{"c":"C"}}{"x":33}{"asd":889}' | jq -c inputs

output:

{"x":33}
{"asd":889}

How to read only first object from input stream and do not touch the rest objects?

Workaround

While writing this Q I found a workaround, but it looks cumbersome

echo '{"a":0,"b":{"c":"C"}}{"x":33}{"asd":889}' | jq -c . | head -1

simply get first line...

标签: json shell jq
1条回答
等我变得足够好
2楼-- · 2019-07-14 03:31

Slurping should, in general, be avoided if possible. If your jq has input, you could simply write:

echo '{"a":0,"b":{"c":"C"}}{"x":33}{"asd":889}' |
  jq -n input

If your jq does not have input, now would be a great time to upgrade to jq 1.6. If that is not an option, then by all means use the -s option, e.g. jq -s '.[0]'

查看更多
登录 后发表回答