Skip to content

Instantly share code, notes, and snippets.

@drewkerrigan
Created October 11, 2021 21:36
Show Gist options
  • 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": "[email protected]",
    "state": "A"
  },
  {
    "name": "First2 Last",
    "email": "[email protected]",
    "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: [email protected]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment