Home > OS >  Sort array by two fields in different levels
Sort array by two fields in different levels

Time:05-02

My input:

[
  {
    "nfStatusNotificationUri": "http://172.19.0.2:32672/callback/nnrf-nfm/v1/onNFStatusEventPost/4e0becf9-c3ec-4002-a32b-2e35b76469b2",
    "subscrCond": {
      "serviceName": "namf-evts"
    },
    "subscriptionId": "36bc52dfdbdd4044b97ef15684706205",
    "validityTime": "2022-04-30T16:40:48.274Z",
    "reqNotifEvents": [
      "NF_DEREGISTERED",
      "NF_PROFILE_CHANGED",
      "NF_REGISTERED"
    ]
  },
  {
    "nfStatusNotificationUri": "http://172.19.0.2:32672/callback/nnrf-nfm/v1/onNFStatusEventPost/5319def1-af0b-4b7b-a94e-b787e614c065",
    "subscrCond": {
      "serviceName": "nbsf-management"
    },
    "subscriptionId": "e2e904bb52ca4fd6b048841c83a4c38e",
    "validityTime": "2022-04-30T16:40:48.26Z",
    "reqNotifEvents": [
      "NF_DEREGISTERED",
      "NF_PROFILE_CHANGED",
      "NF_REGISTERED"
    ]
  },
  {
    "nfStatusNotificationUri": "http://172.19.0.2:32672/callback/nnrf-nfm/v1/onNFStatusEventPost/31dfe10b-4020-47bd-943e-a3e293086b29",
    "subscrCond": {
      "serviceName": "namf-comm"
    },
    "subscriptionId": "e508077fab4f4b8d9dd732176a3777b9",
    "validityTime": "2022-04-30T16:40:48.273Z",
    "reqNotifEvents": [
      "NF_DEREGISTERED",
      "NF_PROFILE_CHANGED",
      "NF_REGISTERED"
    ]
  }
]

I would like to sort it by "subscriptionId" and "serviceName".

I can sort by subscriptionId but I don't know how to specify serviceName to the following expression.

jq -S '.|=sort_by(.subscriptionId)|.[].reqNotifEvents|=sort |del(.[].subscriptionId, .[].validityTime, .[].nfStatusNotificationUri)'

CodePudding user response:

i don't have enough reputation to leave a comment. you might checkout out grouping...

Sorting and filtering a json file using jq

CodePudding user response:

You can parameterize sort_by by a list of keys like so:

sort_by(.subscriptionId, .subscrCond.serviceName)

Online demo

  • Related