Support Questions
Find answers, ask questions, and share your expertise
Announcements
Check out our newest addition to the community, the Cloudera Innovation Accelerator group hub.

jason and jq help

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

1 ACCEPTED SOLUTION

Rising Star
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.

View solution in original post

11 REPLIES 11

@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.

@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"'

Rising Star
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.

Thank you, I am getting null value.

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

@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

Wow working, thank you so much what is the diffrence between putting in the file and trying directly through curl

Rising Star

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).

@Anpan K Please take a moment to login and click the "accept" link on my answer answer.

Rising Star

Is it possible that something in your original redirection to the file was 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). Just to be thorough, could you attach the original file?

one of them shows as unhealthy, how do I find out which component or service is unhealthy in command line.

"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