Created on 05-31-2018 08:37 PM - edited 09-16-2022 06:17 AM
I need jason jq help. I have the health status, but I need to grep the few field only using jq
{ "href" : "http://xxxx:8080/api/v1/clusters/?fields=Clusters/health_report", "items" : [ { "href" : "http://xxxxx:8080/api/v1/clusters/xxxxx", "Clusters" : { "cluster_name" : "xxxx", "health_report" : { "Host/stale_config" : 0, "Host/maintenance_state" : 13, "Host/host_state/HEALTHY" : 13, "Host/host_state/UNHEALTHY" : 0, "Host/host_state/HEARTBEAT_LOST" : 0, "Host/host_state/INIT" : 0, "Host/host_status/HEALTHY" : 12, "Host/host_status/UNHEALTHY" : 1, "Host/host_status/UNKNOWN" : 0, "Host/host_status/ALERT" : 0 }, "version" : "HDP-2.6" } } ]
I only want to grep Host/host_status/UNHEALTHY and it's value. How can I do using jq command
Created 05-31-2018 09:09 PM
jq '.items[].Clusters.health_report."Host/host_status/UNHEALTHY"'
should work. The key name Host/host_status/UNHEALTHY should be quoted to escape the forward slashes in the name.
Created 05-31-2018 09:01 PM
@Anpan K What about this:
jq '.items[0].Clusters.health_report'
HTH
*** If you found this answer addressed your question, please take a moment to login and click the "accept" link on the answer.
Created 05-31-2018 09:04 PM
@Anpan K If you need the value just for "Host/host_status/UNHEALTHY" you can use this one:
jq '.items[0].Clusters.health_report."Host/host_status/UNHEALTHY"'
Created 05-31-2018 09:09 PM
jq '.items[].Clusters.health_report."Host/host_status/UNHEALTHY"'
should work. The key name Host/host_status/UNHEALTHY should be quoted to escape the forward slashes in the name.
Created 05-31-2018 09:10 PM
Thank you, I am getting null value.
Created 05-31-2018 09:18 PM
thanks a lot , but here is the output
]$ cat file1 | jq '.items[].Clusters.health_report."Host/host_status/UNHEALTHY" '
jq: error (at <stdin>:19): Cannot iterate over null (null) ]
$ cat file1 | jq '.items[0].Clusters.health_report."Host/host_status/UNHEALTHY" '
null
Created 05-31-2018 09:21 PM
@Anpan K could you share the content of file1? I have run the following successfully
curl -u admin:admin -X GET 'http://localhost:8080/api/v1/clusters/?fields=Clusters/health_report' | jq '.items[0].Clusters.health_report."Host/host_status/UNHEALTHY"'
HTH
Created 05-31-2018 09:35 PM
Wow working, thank you so much what is the diffrence between putting in the file and trying directly through curl
Created 05-31-2018 09:36 PM
Is it possible that something in your original redirection to the file is removing a character or two? As pasted into your original question, it's invalid JSON, which would also give you an error (since jq would be trying to parse a non-parseable string).
Created 05-31-2018 09:37 PM
@Anpan K Please take a moment to login and click the "accept" link on my answer answer.