【问题标题】:Index each json file then combine into one json with jq索引每个 json 文件,然后用 jq 组合成一个 json
【发布时间】:2021-12-06 07:52:08
【问题描述】:

我有一个json文件的目录,例如:

mock1.json

[
    { "name": "John" },
    { "name": "Mary" }
]

mock2.json

[
    { "name": "Nick" },
    { "name": "Luke" }
]

我知道了

jq -r 'to_entries'

将格式转换为:

[
    {
        "key": 0,
        "value": { "name": "John" }
    },
    {
        "key": 1,
        "value": { "name": "Mary" }
    }
]

我还了解到:

jq -s add [PATH/*.json]

将指定路径下的所有json文件合并为一个json对象。

但是,我正在努力索引 json 文件然后将它们组合起来。比如:

[
    {
        "key": 0,
        "value": { "name": "John" }
    },
    {
        "key": 1,
        "value": { "name": "Mary" }
    },
    {
        "key": 0,
        "value": { "name": "Nick" }
    },
    {
        "key": 1,
        "value": { "name": "Luke" }
    }
]

提前致谢。

【问题讨论】:

    标签: json merge jq


    【解决方案1】:

    这里有两种方法:

    cat mock[12].json | jq -s 'map(to_entries) | flatten'
    

    或者:

    jq to_entries mock[12].json | jq -s flatten
    

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多