【问题标题】:jq add character before and after every row JSONjq 在每行 JSON 前后添加字符
【发布时间】:2022-09-22 22:59:15
【问题描述】:

我想用 jq 在每一行打印 \"[]\" 以制作大文件 json 证明

我有:

{\"test\":\"index\",\"test2\":\"fdsdfs\",\"test3\":\"dfs0D0WOQAA3\"}
{\"test\":\"index\",\"test2\":\"fdsdfs\",\"test3\":\"dfs0D0WOQAA3\"}
{\"test\":\"index\",\"test2\":\"fdsdfs\",\"test3\":\"dfs0D0WOQAA3\"}
{\"test\":\"index\",\"test2\":\"fdsdfs\",\"test3\":\"dfs0D0WOQAA3\"}

我想要的是:

[[{\"test\":\"index\",\"test2\":\"fdsdfs\",\"test3\":\"dfs0D0WOQAA3\"}]
[{\"test\":\"index\",\"test2\":\"fdsdfs\",\"test3\":\"dfs0D0WOQAA3\"}]
[{\"test\":\"index\",\"test2\":\"fdsdfs\",\"test3\":\"dfs0D0WOQAA3\"}]
[{\"test\":\"index\",\"test2\":\"fdsdfs\",\"test3\":\"dfs0D0WOQAA3\"}]]

现在我可以将json逐行推送到程序中。

  • 显示的输出是不是一个有效的 JSON。更新您的问题以反映实际情况。你的意思是显示一个数组数组吗?

标签: json set transform jq strip


【解决方案1】:

如果您输入的只是普通对象:
使用--slurp创建单个数组,然后使用map([ . ])将每个对象包装在自己的数组中

jq --slurp --compact-output 'map([ . ])'
[[{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}],[{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}], ...

JqPlay demo

【讨论】:

    【解决方案2】:

    假设您的文件包含一个独立的 JSON 对象流,则过滤器就是 [.]。过滤器将应用于每个输入对象。

    jq -c '[.]' file.json
    

    输出:

    [{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}]
    [{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}]
    [{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}]
    [{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}]
    

    要将所有内容包装在一个大数组中:

    jq -s . file.json
    # or:
    jq -s <file.json
    

    输出:

    [
      {
        "test": "index",
        "test2": "fdsdfs",
        "test3": "dfs0D0WOQAA3"
      },
      {
        "test": "index",
        "test2": "fdsdfs",
        "test3": "dfs0D0WOQAA3"
      },
      {
        "test": "index",
        "test2": "fdsdfs",
        "test3": "dfs0D0WOQAA3"
      },
      {
        "test": "index",
        "test2": "fdsdfs",
        "test3": "dfs0D0WOQAA3"
      }
    ]
    

    并将每个单个项目包装在一个数组中,并将所有数组包装在一个顶级数组中:

    jq 'map([.])' file.json
    

    输出:

    [
      [
        {
          "test": "index",
          "test2": "fdsdfs",
          "test3": "dfs0D0WOQAA3"
        }
      ],
      [
        {
          "test": "index",
          "test2": "fdsdfs",
          "test3": "dfs0D0WOQAA3"
        }
      ],
      [
        {
          "test": "index",
          "test2": "fdsdfs",
          "test3": "dfs0D0WOQAA3"
        }
      ],
      [
        {
          "test": "index",
          "test2": "fdsdfs",
          "test3": "dfs0D0WOQAA3"
        }
      ]
    ]
    

    【讨论】:

      【解决方案3】:

      我假设您只是忘记将逗号放在其他有效的 JSON 输出中,在这种情况下,您可以在此页面的其他地方找到合理的答案。

      如果不是,并且您想要的输出是准确的,如图所示,使用 jq 解决它的一种方法是将行作为原始文本读取,在您认为合适的情况下添加并附加到某些行,然后再次作为原始文本输出:

      jq -Rnr '[inputs] | (first, .[]) |= "[" + . | (last, .[]) += "]" | .[]'
      
      [[{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}]
      [{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}]
      [{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}]
      [{"test":"index","test2":"fdsdfs","test3":"dfs0D0WOQAA3"}]]
      

      Demo

      但是,公平地说,如果只是处理纯文本,您实际上不需要 JSON 处理器。在这里使用sedawk 会是更合理的选择。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-07
        • 2011-01-07
        • 1970-01-01
        相关资源
        最近更新 更多