使用 JQ 键合并两个 Json

Combine two Json's by key using JQ

我有两个 json,它们是共享相同键的对象列表,我正尝试使用 jq 将它们组合成一个 json。预期输出是单个 json,其中包含列表形式的组合对象列表。例如:

Json 1:

[
{"Id":"1", "FirstName":"firstName1", "LastName":"lastName1"},
{"Id":"2", "FirstName":"firstName2", "LastName":"lastName2"},
{"Id":"3", "FirstName":"firstName2", "LastName":"lastName3"}
]

Json 2:

[
{"School":"School1", "Id":"1", "Degree":"Degree1"},
{"School":"School2", "Id":"2", "Degree":"Degree2"},
{"School":"School3", "Id":"3", "Degree":"Degree3"}
]

合并Json 基于 Id

[
{"Id":"1", "FirstName":"firstName1", "LastName":"lastName1",
"School":"School1", "Degree":"Degree1"},
{"Id":"2", "FirstName":"firstName2", "LastName":"lastName2",
"School":"School2", "Degree":"Degree2"},
{"Id":"3", "FirstName":"firstName2", "LastName":"lastName3",
"School":"School3", "Degree":"Degree3"}
]

我已经尝试过几种方法来合并这些 json 我 found in this thread 例如:

jq -s '.[0] * .[1]' file1 file2

我还是jq的新手,所以任何帮助将不胜感激!

使用 SQL-Style Operators JOININDEX

jq 'JOIN(INDEX(inputs[];.Id);.[];.Id;add)' json1 json2
[
  {
    "Id": "1",
    "FirstName": "firstName1",
    "LastName": "lastName1",
    "School": "School1",
    "Degree": "Degree1"
  },
  {
    "Id": "2",
    "FirstName": "firstName2",
    "LastName": "lastName2",
    "School": "School2",
    "Degree": "Degree2"
  },
  {
    "Id": "3",
    "FirstName": "firstName2",
    "LastName": "lastName3",
    "School": "School3",
    "Degree": "Degree3"
  }
]

Demo