如何使用jq输出JSONL(每行一个独立的JSON对象) [英] How to use jq to output JSONL (one independent JSON object per line)

查看:344
本文介绍了如何使用jq输出JSONL(每行一个独立的JSON对象)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求听起来微不足道,但我找不到解决方法.作为输入,我有一个JSON对象数组:

My request sounds trivial but I could not find a way to do it. I have as input an array of JSON objects:

[
    {
        "foo": 1,
        "bar": 2
    },
    {
        "foo": 3,
        "bar": 4
    },
    (...)
]

并且我想要输出相同的JSONL版本,也就是每行一个对象,而不是数组:

and I want as output the JSONL version of the same, aka one object per line, not an array:

    { "foo": 1, "bar": 2 }
    { "foo": 3, "bar": 4 }
    (...)

这与使用--compact-output 相同,因为那样可以保留数组并给我:

This is not the same as using --compact-output, as that would preserve the array and give me:

    [ { "foo": 1, "bar": 2 }, { "foo": 3, "bar": 4 }, (...) ]

谢谢.

推荐答案

原始问题的答案是将过滤器.[]-c命令行选项一起使用:

The answer to the original question is to use the filter .[] together with the -c command-line option:

$ jq -c '.[]'

这篇关于如何使用jq输出JSONL(每行一个独立的JSON对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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