如何使用jq输出JSONL(每行一个独立的JSON对象)

gia*_*cco 14 json jq jsonlines

我的请求听起来微不足道,但我找不到办法.我输入了一个JSON对象数组:

[
    {
        "foo": 1,
        "bar": 2
    },
    {
        "foo": 3,
        "bar": 4
    },
    (...)
]
Run Code Online (Sandbox Code Playgroud)

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

    { "foo": 1, "bar": 2 }
    { "foo": 3, "bar": 4 }
    (...)
Run Code Online (Sandbox Code Playgroud)

这是一样的使用--compact-output,因为这将保护阵列,并给我:

    [ { "foo": 1, "bar": 2 }, { "foo": 3, "bar": 4 }, (...) ]
Run Code Online (Sandbox Code Playgroud)

先感谢您.

pea*_*eak 17

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

$ jq -c '.[]'
Run Code Online (Sandbox Code Playgroud)


gia*_*cco -2

发现:是

map(tostring) | reduce .[] as $item (""; . + $item + "\n")
Run Code Online (Sandbox Code Playgroud)

您还需要使用--raw-output.