TLDR1
jq has been a challenging tool for me for quite some time. As humans, we still struggle with handling API responses.
Like many developers, I too have encountered difficulties with the syntax of jq. I strongly believe this tool is powerful but not for humans. 🙂
Although gron2 has long been considered a more user-friendly tool, it falls short in comparison to jq.
When doing my work , I prefer using tools that align well with my thought process and make it easier to create scenarios that improve life.
To free up more time, I have been contemplating which tasks I should delegate or assign to AI, and one area where I strongly believe. Writing jq rules is the one of them.
AI + jq + gron = ❤️❤️❤️
Let’s explain the problem to the AI.
Problem
I want to find whole clusters which has any UNHEALTY status.
Search one cluster name which I know
1 2 3 |
curl apisource > /tmp/api-response cat /tmp/api-response | gron | grep ClusterA json.clusters[37].name = "ClusterA"; |
Find possible statuses for the health
1 2 3 4 5 6 7 |
cat /tmp/api-response | gron | grep HEALTHY json.clusters[38].healthStatus.admissionControlHealthStatus = "HEALTHY"; json.clusters[38].healthStatus.collectorHealthStatus = "HEALTHY"; json.clusters[38].healthStatus.overallHealthStatus = "HEALTHY"; json.clusters[38].healthStatus.scannerHealthStatus = "HEALTHY"; json.clusters[38].healthStatus.sensorHealthStatus = "HEALTHY"; |
Explain everything to the AI!!
This is my API structure
1 2 3 4 5 6 |
json.clusters[37].name = "tst-wus2-ahcrs01a"; json.clusters[38].healthStatus.admissionControlHealthStatus = "HEALTHY"; json.clusters[38].healthStatus.collectorHealthStatus = "HEALTHY"; json.clusters[38].healthStatus.overallHealthStatus = "HEALTHY"; json.clusters[38].healthStatus.scannerHealthStatus = "HEALTHY"; json.clusters[38].healthStatus.sensorHealthStatus = "HEALTHY"; |
Write me jq command if collectorHealthStatus or admissionControlHealthStatus or scannerHealthStatus or overallHealthStatus or sensorHealthStatus not equal to HEALTY print .name
Result is here!!
1 |
cat /tmp/api-response | jq '.clusters[] | select(.healthStatus.collectorHealthStatus != "HEALTHY" or .healthStatus.admissionControlHealthStatus != "HEALTHY" or .healthStatus.scannerHealthStatus != "HEALTHY" or .healthStatus.overallHealthStatus != "HEALTHY" or .healthStatus.sensorHealthStatus != "HEALTHY").name' |
2. Advantages of using this
- We don’t give whole json which contains unnecessary information3
- No Typo!! 4
- This way allow us to automate everything easily