Skip to content

Instantly share code, notes, and snippets.

@boddumanohar
Last active December 17, 2022 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boddumanohar/ff22099b19115cb9832e8a7db7394ba1 to your computer and use it in GitHub Desktop.
Save boddumanohar/ff22099b19115cb9832e8a7db7394ba1 to your computer and use it in GitHub Desktop.
Extract a specific field from JSON output using jq
{
  "example": {
    "sub-example": [
      {
        "name": "123-345",
        "tag" : 100
      },
      {
        "name": "234-456",
        "tag" : 100
      },
      {
        "name": "4a7-a07a5",
        "tag" : 100
      }
    ]
  }
}

If you just want to extract the name fields, the command you're looking for is jq '.example."sub-example" | .[] | .name'. If you want to keep the names in an array, wrap the whole jq expression in square brackets.

to get length

jq '.example."sub-example" | length'

@boddumanohar
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment