0

is it possible to capture the required tag value by jq

for example we want to capture the tag value - version1531837630468 while the input is ams-env

so jq will capture the tag value by ams-env

{
  "href" : "http://master:8080/api/v1/clusters/HDP?fields=Clusters/desired_configs",
  "Clusters" : {
    "cluster_name" : "HDP",
    "version" : "HDP-2.6",
    "desired_configs" : {
      "airflow" : {
        "tag" : "version1508837789535411",
        "version" : 2
      },
      "ams-env" : {
        "tag" : "version1531837630468",
        "version" : 7
      },
      "ams-grafana-env" : {
        "tag" : "version1531831836137",
        "version" : 5
      },
      .
      .
      .
      .
      .
      .

1 Answer 1

3
$ jq -r '.Clusters.desired_configs."ams-env".tag' file.json
version1531837630468

The ams-env bit has to be quoted as it contains a minus sign. Apart from that, it's straightforward.

If you want to pass ams-env as a variable on the command line:

$ jq -r --arg var "ams-env" '.Clusters.desired_configs[$var].tag' file.json
version1531837630468
4
  • what in case we want to set the ams-env in variable for example - VAR=ams-env , then what should be the jq syntax ?
    – yael
    Commented Aug 6, 2018 at 12:51
  • when I run it as ./jq -r '.Clusters.desired_configs."$VAR".tag' file.json , it not give any output
    – yael
    Commented Aug 6, 2018 at 12:57
  • I try also this but without results - jq -r --arg VAR "hive-log4j" '.Clusters.desired_configs."$VAR".tag' file.json
    – yael
    Commented Aug 6, 2018 at 13:12
  • @yael See updated answer.
    – Kusalananda
    Commented Aug 6, 2018 at 13:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .