'Query JSONPath in JQ Format

I have JSONPath:

$.endpointAgents[?(@.clients.userName=~ 'a')].agentId

How it will look in jq format on Linux??

jq '.endpointAgents [] | select(.clients.userName=~"a") | {agentId}')"

does not work.

Code:

{
  "endpointAgents": [
    {
      "agentId": "MyId",
      "agentName": "MYNAME",
      "location": {
        "locationName": "location"
      },
      "clients": [
        {
          "userProfile": {
            "userName": "Name"
          },
          "browserExtensions": [
            {
              "active": false
            }
          ]
        }
      ],
      "totalMemory": "16222 MB",
      "agentType": "enterprise"
    }
  ]
}


Solution 1:[1]

If I understand correctly, you want to produce the agent id of endpoints with a client whose name matches a.

.endpointAgents[] |
select( any( .clients[].userProfile.userName; test("a") ) ) |
.agentId

To produce the agent id of endpoints with a client whose name is equal to a, use the following instead:

.endpointAgents[] |
select( any( .clients[].userProfile.userName; . == "a" ) ) |
.agentId

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1