将两个文件中的对象数组与特定键 1.4 下的 jq 结合起来

2024-01-12

我有两个具有以下 JSON 的文件,我需要使用每个对象的相对数组位置来组合它们:

PS: - 我被限制为 1.4 版本,就像在 Solaris 上一样,所以没有 [inputs] 功能

File 1

{
  "input": [
    {
      "email": "[email protected] /cdn-cgi/l/email-protection",
      "firstName": "Fred"
    },
    {
      "email": "[email protected] /cdn-cgi/l/email-protection",
      "firstName": "James"
    }
  ]
}

File 2:

{
  "result": [
    {
      "id": 50,
      "status": "created"
    },
    {
      "id": 51,
      "status": "rejected"
    }
  ]
}

预期结果是 input[1] 的元素与 result[1] 的元素相结合,依此类推,如下所示:

{
  "combined": [
    {
      "email": "[email protected] /cdn-cgi/l/email-protection",
      "firstName": "Fred",
      "id": 50,
      "status": "created"
    },
    {
      "email": "[email protected] /cdn-cgi/l/email-protection",
      "firstName": "James",
      "id": 51,
      "status": "rejected"
    }
  ]
}

您可以使用--slurp选项将两个文件读入一个数组,然后循环遍历其中一个数组的键并将两个数组的相应元素添加在一起相对简单。

jq --slurp '
{
  combined: [
    .[0].input as $is|
    .[1].result as $rs|
    range(0; $is|length) as $n|
    $is[$n]+$rs[$n]
  ]
}
' file1.json file2.json
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将两个文件中的对象数组与特定键 1.4 下的 jq 结合起来 的相关文章

随机推荐