Skip to content

Instantly share code, notes, and snippets.

@hhls
Forked from ipbastola/jq to filter by value.md
Created April 21, 2019 10:33
Show Gist options
  • Save hhls/a0df2efd6dea2bac9bc162b3acd33a6c to your computer and use it in GitHub Desktop.
Save hhls/a0df2efd6dea2bac9bc162b3acd33a6c to your computer and use it in GitHub Desktop.
JQ to filter JSON by value

JQ to filter JSON by value

Syntax: cat <filename> | jq -c '.[] | select( .<key> | contains("<value>"))'

Example: To get json record having _id equal 611

cat my.json | jq -c '.[] | select( ._id | contains(611))'

Remember: if JSON value has no double quotes (eg. for numeric) to do not supply in filter i.e. in contains(611)

@hhls
Copy link
Author

hhls commented Apr 21, 2019

{
    "status":0,
    "message":"success",
    "data":{
        "result":{
            "a.xx.server":[
                "1.xxx.com",
                "2.xxx.com"
            ],
            "b.xx.server":[
                "1.xxx.com",
                "2.xxx.com"
            ]
        }
    }
}
#!/bin/sh
appname=$1
curl -s -d '{"env":0}' -H 'content-type: application/json' https://XXX/servers | jq -r -c '.data.result | with_entries(select(.key|contains("'$appname'")))'

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