Senthil
Senthil

Reputation: 101

Need help parsing json output with jq for a complex json

For the below JSON, I need the result.id and result.name output using jq for the ones having

authorization.roles[].name == "Supervisor" 

What is the command for jq to to that ? For the below json we expect 1231 id and name AAAA alone as output as that only has Supervisor as role

{
    "results": [{
        "id": "1231",
        "name": "AAAA",
        "div": {
            "id": "AAA",
            "name": "DDSAA",
            "selfUri": ""
        },
        "chat": {
            "jabberId": "nn"
        },
        "department": "Shared Services Organization",
        "email": "[email protected]",
        "primaryContactInfo": [{
            "address": "[email protected]",
            "mediaType": "EMAIL",
            "type": "PRIMARY"
        }],
        "addresses": [],
        "state": "active",
        "title": "AAA",
        "username": "[email protected]",
        "version": 27,
        "authorization": {
            "roles": [{
                "id": "01256689-c5ed-43a5-b370-58522402830d",
                "name": "AA"
            }, {
                "id": "1e65b009-9f8f-4eef-9844-83944002c095",
                "name": "BBB"
            }, {
                "id": "8a19f1ff-40e5-45d2-b758-14550a173323",
                "name": "CCC"
            }, {
                "id": "d02250e2-7071-46bf-885b-43edff2d88a6",
                "name": "Supervisor"
            }]
        }
    }, {
        "id": "1255",
        "name": "BBBB",
        "div": {
            "id": "AAA",
            "name": "DDSAA",
            "selfUri": ""
        },
        "chat": {
            "jabberId": "nn"
        },
        "department": "Shared Services Organization",
        "email": "[email protected]",
        "primaryContactInfo": [{
            "address": "[email protected]",
            "mediaType": "EMAIL",
            "type": "PRIMARY"
        }],
        "addresses": [],
        "state": "active",
        "title": "AAA",
        "username": "[email protected]",
        "version": 27,
        "authorization": {
            "roles": [{
                "id": "01256689-c5ed-43a5-b370-58522402830d",
                "name": "AA"
            }, {
                "id": "1e65b009-9f8f-4eef-9844-83944002c095",
                "name": "BBB"
            }, {
                "id": "8a19f1ff-40e5-45d2-b758-14550a173323",
                "name": "CCC"
            }, {
                "id": "d02250e2-7071-46bf-885b-43edff2d88a6",
                "name": "Tester"
            }]
        }
    }]
}

Upvotes: 0

Views: 118

Answers (1)

pmf
pmf

Reputation: 36078

Don't put commas before closing brackets or curly braces (it's not valid JSON). Your input should look like this:

{
  "results": [
    {
      "id": "1231",
      "name": "AAAA",
      "div": {
        "id": "AAA",
        "name": "DDSAA",
        "selfUri": ""
      },
      "chat": {
        "jabberId": "nn"
      },
      "department": "Shared Services Organization",
      "email": "[email protected]",
      "primaryContactInfo": [
        {
          "address": "[email protected]",
          "mediaType": "EMAIL",
          "type": "PRIMARY"
        }
      ],
      "addresses": [],
      "state": "active",
      "title": "AAA",
      "username": "[email protected]",
      "version": 27,
      "authorization": {
        "roles": [
          {
            "id": "01256689-c5ed-43a5-b370-58522402830d",
            "name": "AA"
          },
          {
            "id": "1e65b009-9f8f-4eef-9844-83944002c095",
            "name": "BBB"
          },
          {
            "id": "8a19f1ff-40e5-45d2-b758-14550a173323",
            "name": "CCC"
          },
          {
            "id": "d02250e2-7071-46bf-885b-43edff2d88a6",
            "name": "Supervisor"
          }
        ]
      }
    },
    {
      "id": "1255",
      "name": "BBBB",
      "div": {
        "id": "AAA",
        "name": "DDSAA",
        "selfUri": ""
      },
      "chat": {
        "jabberId": "nn"
      },
      "department": "Shared Services Organization",
      "email": "[email protected]",
      "primaryContactInfo": [
        {
          "address": "[email protected]",
          "mediaType": "EMAIL",
          "type": "PRIMARY"
        }
      ],
      "addresses": [],
      "state": "active",
      "title": "AAA",
      "username": "[email protected]",
      "version": 27,
      "authorization": {
        "roles": [
          {
            "id": "01256689-c5ed-43a5-b370-58522402830d",
            "name": "AA"
          },
          {
            "id": "1e65b009-9f8f-4eef-9844-83944002c095",
            "name": "BBB"
          },
          {
            "id": "8a19f1ff-40e5-45d2-b758-14550a173323",
            "name": "CCC"
          },
          {
            "id": "d02250e2-7071-46bf-885b-43edff2d88a6",
            "name": "Tester"
          }
        ]
      }
    }
  ]
}

Then, you can use select to narrow down your target objects (here using any to check if at least one of the role names matches your string -- thx @ikegami), then output any part of the resulting object(s):

jq '
  .results[]
  | select(any(.authorization.roles[]; .name == "Supervisor"))
  | {id, name}
'
{
  "id": "1231",
  "name": "AAAA"
}

Demo

If instead of a JSON output you need raw text, use the -r (or --raw-output) flag, and provide the fields you are interested in:

jq -r '
  .results[]
  | select(any(.authorization.roles[]; .name == "Supervisor"))
  | .id, .name
'
1231
AAAA

Demo

Upvotes: 1

Related Questions