重复的 ID 会产生一些空值

duplicate ids are coming into result with some null values

我有下面的 json,我只想过滤掉唯一的 product.id 到数组变量中。

我正在使用以下查询,但结果不正确。

productIds=($(jq -r .items[].product.id $Outputfile))

实际输出: 123 123 123 456 456 无效的 空

预期输出: 123 456

{
  "documentType": "product",
  "items": [
    {
      "ResourceId": null,
      "product": {
        "href": null,
        "id": "123",
        "mainId": "qwe345",
        "primaryId": "5298"
      },
      "lastModifiedBy": "test",
      "quantity": null,
      "effectiveDate": null,
      "extensions": null
    },
    {
      "product": {
        "href": null,
        "id": "123",
        "mainId": "qwe678",
        "primaryId": "5643"
      },
      "lastModifiedBy": "test",
      "quantity": null,
      "effectiveDate": null,
      "extensions": null
    }
  ],
  "createdBy": "test",
  "createdOn": "2021-10-05",
  "currentSeqNum": 2
}

然后我想将这些值编码为 base64 格式,然后与一些常量值连接。我也无法与常量连接并存储在循环内的相同变量中。

你能帮我解决以上两个问题吗

你可以在应用数组转换后使用unique然后join

productIds=$(jq -r '[.items[].product.id] | unique | join(" ")' $Outputfile)

Demo