使用jq将带有换行符的文本文件转换为json [英] Convert text file with line breaks to json with jq

查看:260
本文介绍了使用jq将带有换行符的文本文件转换为json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了许多使用jq将文本文件转换为json的示例,但是我陷入了一些显而易见的问题.我的输入文件具有以下格式:

I've seen a lot of examples to convert a text file to json with jq, but I get stuck on something probably obvious. My input file has this format:

key1: string1
key2: string1

key1: string3
key2: string3

如何将其翻译为:

[
  {"key1":"string1", "key2": "string2"},
  {"key1":"string3", "key2": "string4"}
]

我试图将inputs与jq一起使用,类似于jq -R -n -c '[inputs|split(":")|{(.[0]):.[1]}] | add',但是一旦文件中出现换行符,它就会失败:

I've tried to use inputs with jq, something like jq -R -n -c '[inputs|split(":")|{(.[0]):.[1]}] | add', but it fails as soon as there's a line break in the file:

jq: error (at result.txt:8): Cannot use null (null) as object key.

谢谢

推荐答案

这是reducer的一个很好的用例.

This is a pretty good use case for a reducer.

在其标准输入(脚本)上运行文档:

Run with your document on its stdin, the script:

#!/usr/bin/env jq -Rncf
# ...caveat: not all platforms support more than two words on the shebang
#            you may need to stop using env and pass a full path to your jq binary!

def input_line_to_object:
  "^(?<key>[^:]+): ?(?<value>.*)$" as $re |
  (capture($re) // ("line \(.) does not parse" | halt_error(1))) | {(.key):.value};

reduce (inputs, "") as $item (
  # Initializer. .[0] == current object; .[1] == list of objects already constructed
  [{}, []];
  # Handler. Run for each line, and the final "" at the end.
  if $item == "" then
    if .[0] == {} then
      . # no new object is actually ready; maybe we had two newlines in a row?
    else
      # between objects: append .[0] to .[1], and clear .[0]
      [{}, .[1]+[.[0]]]
    end
  else
    # new data in an existing object; add new datum to .[0], leave .[1] alone
    [.[0] + ($item | input_line_to_object), .[1]]
  end
) | .[1] # take the completed list in the .[1] position

...作为输出发出:

...emits as output:

[{"key1":"string1","key2":"string1"},{"key1":"string3","key2":"string3"}]

这篇关于使用jq将带有换行符的文本文件转换为json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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