用jq拆分多个输入JSON [英] Split multiple input JSONs with jq

查看:96
本文介绍了用jq拆分多个输入JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出JSON行

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

由3个独立的JSON对象组成.

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

预期输出:

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

我找到了唯一可以删除第一个对象并输出其他对象的命令. inputs

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

输出:

{"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

仅获得第一行...

推荐答案

如果可能的话,一般应该避免拖拉.如果您的jq具有input,则可以简单地写:

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

如果您的jq没有input,那么现在是升级到jq 1.6的好时机.如果这不是一个选项,则一定要使用-s选项,例如jq -s '.[0]'

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]'

这篇关于用jq拆分多个输入JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆