Support Questions

Find answers, ask questions, and share your expertise

jason and jq help

avatar

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

avatar
Expert Contributor
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

avatar

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

avatar

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

avatar
Expert Contributor
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.

avatar

Thank you, I am getting null value.

avatar

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

avatar

@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

avatar

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

avatar
Expert Contributor

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

avatar

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