json JQ -分组重复,而不是删除它们

fsi0uk1n  于 2023-08-08  发布在  其他
关注(0)|答案(1)|浏览(97)

我试图通过extId将以下json中的重复项分组,但删除重复项。
(有2个'extId',我只对'users'里面的那个感兴趣)

这里是json:

{
  "intId": "97210f2c23f45b55569f32309105f395c94",
  "extId": "39e9588ee62d1cccc711d75ff86c5570",
  "name": "generic name",
  "users": {
    "186-1": {
      "userKey": "186-1",
      "extId": "186",
      "intIds": {
        "w_eyvyfghz844o": {
          "intId": "w_eyvyfghz844o",
          "registeredOn": 1690289980777,
          "leftOn": 0,
          "userLeftFlag": false
        }
      },
      "name": "generic name 1",
      "isModerator": true,
      "isDialIn": false,
      "currentIntId": "w_eyvyfghz844o",
      "answers": {},
      "talk": {
        "totalTime": 5696194,
        "lastTalkStartedOn": 1690297856252
      },
      "emojis": [],
      "webcams": [
        {
          "startedOn": 1690289988531,
          "stoppedOn": 0
        }
      ],
      "totalOfMessages": 18
    },
    "95-1": {
      "userKey": "95-1",
      "extId": "95",
      "intIds": {
        "w_kegd1ecwju02": {
          "intId": "w_kegd1ecwju02",
          "registeredOn": 1690295357122,
          "leftOn": 1690295379924,
          "userLeftFlag": true
        },
        "w_nc1egwb2axbr": {
          "intId": "w_nc1egwb2axbr",
          "registeredOn": 1690296171797,
          "leftOn": 0,
          "userLeftFlag": false
        }
      },
      "name": "Generic name 2",
      "isModerator": true,
      "isDialIn": false,
      "currentIntId": "w_nc1egwb2axbr",
      "answers": {},
      "talk": {
        "totalTime": 0,
        "lastTalkStartedOn": 0
      },
      "emojis": [],
      "webcams": [],
      "totalOfMessages": 0
    },
    "95-2": {
      "userKey": "95-2",
      "extId": "95",
      "intIds": {
        "w_et8i4fc0fumu": {
          "intId": "w_et8i4fc0fumu",
          "registeredOn": 1690293905345,
          "leftOn": 1690294109923,
          "userLeftFlag": true
        }
      },
      "name": "Generic name 2",
      "isModerator": true,
      "isDialIn": false,
      "answers": {},
      "talk": {
        "totalTime": 0,
        "lastTalkStartedOn": 0
      },
      "emojis": [],
      "webcams": [],
      "totalOfMessages": 0
    }
  },
  "polls": {},
  "screenshares": [
    {
      "startedOn": 1690290408449,
      "stoppedOn": 1690290573091
    },
    {
      "startedOn": 1690294824305,
      "stoppedOn": 1690294957331
    },
    {
      "startedOn": 1690294976792,
      "stoppedOn": 1690295106468
    },
    {
      "startedOn": 1690295120270,
      "stoppedOn": 1690295742503
    },
    {
      "startedOn": 1690295747135,
      "stoppedOn": 1690297603487
    },
    {
      "startedOn": 1690297609337,
      "stoppedOn": 0
    }
  ],
  "presentationSlides": [
    {
      "presentationId": "8b8e93774c2cebfcde9582e0f9c169",
      "pageNum": 1,
      "setOn": 1690288860858
    }
  ],
  "createdOn": 1690288854908,
  "endedOn": 0
}

字符串
以下是我尝试过的:

jq -r '.users | [.[] | { "extId":(.extId),"name":.name, "conections":[( .intIds[] | {"registeredOn":.registeredOn,"leftOn":.leftOn,"userLeftFlag":.userLeftFlag} )] } ] | INDEX(.extId)'
{
  "186": {
    "extId": "186",
    "name": "generic name 1",
    "conections": [
      {
        "registeredOn": 1690289980777,
        "leftOn": 0,
        "userLeftFlag": false
      }
    ]
  },
  "95": {
    "extId": "95",
    "name": "Generic name 2",
    "conections": [
      {
        "registeredOn": 1690293905345,
        "leftOn": 1690294109923,
        "userLeftFlag": true
      }
    ]
  }
}

的数据
但是使用INDEX(.extId)删除了重复项,因此我知道需要在

[
  {
    "extId": "186",
    "name": "generic name 1",
    "conections": [
      {
        "registeredOn": 1690289980777,
        "leftOn": 0,
        "userLeftFlag": false
      }
    ]
  },
  {
    "extId": "95",
    "name": "Generic name 2",
    "conections": [
      {
        "registeredOn": 1690295357122,
        "leftOn": 1690295379924,
        "userLeftFlag": true
      },
      {
        "registeredOn": 1690296171797,
        "leftOn": 0,
        "userLeftFlag": false
      }
    ]
  },
  {
    "extId": "95",
    "name": "Generic name 2",
    "conections": [
      {
        "registeredOn": 1690293905345,
        "leftOn": 1690294109923,
        "userLeftFlag": true
      }
    ]
  }
]


我不要求对象有一个键,但我想保留它。
如果你能帮忙的话,我将不胜感激。

预期输出:

[
  "186": {
    "extId": "186",
    "name": "generic name 1",
    "connections": [
      {
        "registeredOn": 1690289980777,
        "leftOn": 0,
        "userLeftFlag": false
      }
    ]
  },
  "95": {
    "extId": "95",
    "name": "Generic name 2",
    "connections": [
      {
        "registeredOn": 1690295357122,
        "leftOn": 1690295379924,
        "userLeftFlag": true
      },
      {
        "registeredOn": 1690296171797,
        "leftOn": 0,
        "userLeftFlag": false
      },
      {
        "registeredOn": 1690293905345,
        "leftOn": 1690294109923,
        "userLeftFlag": true
      }
    ]
  }
]

llew8vvj

llew8vvj1#

如果你在收集.intIDs[]下的对象时尝试按.extId分组,那么group_by必须在外部和内部Map之间进行:

.users
| map({extId, name, connections: .intIds | map({registeredOn, leftOn, userLeftFlag})})
| group_by(.extId)
| map(first + {connections: map(.connections[])})

字符串
Demo
或者,您可以使用reduce进行迭代,通过收集到对象中手动分组,然后使用map(.)恢复数组:

reduce .users[] as $user ({}; .[$user.extId] |= (($user | {extId, name}) + (
  .connections += [$user.intIds[] | {registeredOn, leftOn, userLeftFlag}]
))) | map(.)


Demo
两种方法的输出

[
  {
    "extId": "186",
    "name": "generic name 1",
    "connections": [
      {
        "registeredOn": 1690289980777,
        "leftOn": 0,
        "userLeftFlag": false
      }
    ]
  },
  {
    "extId": "95",
    "name": "Generic name 2",
    "connections": [
      {
        "registeredOn": 1690295357122,
        "leftOn": 1690295379924,
        "userLeftFlag": true
      },
      {
        "registeredOn": 1690296171797,
        "leftOn": 0,
        "userLeftFlag": false
      },
      {
        "registeredOn": 1690293905345,
        "leftOn": 1690294109923,
        "userLeftFlag": true
      }
    ]
  }
]

相关问题