Skip to content

Instantly share code, notes, and snippets.

@drewkerrigan
Created October 11, 2021 21:36
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 drewkerrigan/cddefff412c91d9ad38441ba76951c21 to your computer and use it in GitHub Desktop.
Save drewkerrigan/cddefff412c91d9ad38441ba76951c21 to your computer and use it in GitHub Desktop.
Filtering and Formating JSON with JQ

Create test.json:

cat << 'EOF' > ./test.json
[
  {
    "name": "First1 Last",
    "email": "first1@company.io",
    "state": "A"
  },
  {
    "name": "First2 Last",
    "email": "first2@company.io",
    "state": "S"
  }
]
EOF

Filter on state=A, format output of name and email:

cat test.json | jq -r 'map(select(.state | contains ("A"))) | .[] | .name as $name | .email as $email | "Name: \($name), Email: \($email)"'

Output:

Name: First1 Last, Email: first1@company.io
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment