I have a Json with this kind of records and I want to filter it using jq or grep to get an output with only the full records where "__pod_controller_name" its "podIwant"
{
"endpoint": "http://10.225.25.33:8080/metrics",
"properties": {
"__pod_name": "pod1",
"__pod_controller_name": "podIwant"
}
}
{
"endpoint": "http://10.225.10.21:8080/metrics",
"properties": {
"__pod_name": "pod2",
"__pod_controller_name": "podIdontwant"
}
}
{
"endpoint": "http://10.225.33.52:8080/metrics",
"properties": {
"__pod_name": "pod3",
"__pod_controller_name": "podIwant"
}
}
I’ve been trying with a lot of jq and map commands but I could not.
Thanks.
>Solution :
For structured data like JSON, use appropriate tools like jq, not grep. With jq, you can filter using select. It takes some filter, and if it evaluates to true its input is passed on.
jq 'select(.properties.__pod_controller_name == "podIwant")' input.json
If you want the query string to be dynamic (adjustable from the calling environment), use the --arg flag to bind it to a variable:
jq --arg name 'podIwant' 'select(.properties.__pod_controller_name == $name)' input.json
Output:
{
"endpoint": "http://10.225.25.33:8080/metrics",
"properties": {
"__pod_name": "pod1",
"__pod_controller_name": "podIwant"
}
}
{
"endpoint": "http://10.225.33.52:8080/metrics",
"properties": {
"__pod_name": "pod3",
"__pod_controller_name": "podIwant"
}
}