Home > OS >  Query JSONPath in JQ Format
Query JSONPath in JQ Format

Time:02-02

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"
    }```

CodePudding user response:

I want to get userName value by specifying agentId

jq '.endpointAgents[] | select(.agentId == "MyId") | .clients[].userProfile.userName'

Will output "Name"


  • .endpointAgents[]
    Loop over each endpointAgent

  • select(.agentId == "MyId")
    Select the objects where .agentId == "MyId"

  • .clients[].userProfile.userName
    Since clients is an array, loop over it, and show .userProfile.userName for each object.


Try it online!

  •  Tags:  
  • Related