Home > OS >  Issue with json extraction key value
Issue with json extraction key value

Time:12-29

I have this jq to work with another json file somehow is not picking this up based on key and value. I would like to get the status of each value either UP or DOWN for each key. The code is below:

  curl -s http://example:8080/external_indicator | jq -r '
  to_entries[] | select(.value | type == "object")
  | [.key, .value.components.status] as [$id, $status]
  | "app_custom,\($id=\($status) wlb_status_code=\(["DOWN","UP"] | index($status))"
'

Desired output should be:

                  app_custom,db=UP status_code=1
                  app_custom,angleDS=UP status_code=1
                  app_custom,argosleDS=UP status_code=1

But current output is :

app_custom,components_status=null status_code=null 

Json file is

{
  "status": "UP",
  "components": {
    "db": {
      "status": "UP",
      "components": {
        "DS": {
          "status": "UP",
          "details": {
            "database": "Informix Dynamic Server",
            "validationQuery": "select count(1) from systables",
            "result": 194
          }
        },
        "angusDS": {
          "status": "UP",
          "details": {
            "database": "Informix Dynamic Server",
            "validationQuery": "select count(1) from systables",
            "result": 166
          }
        },
        "argyleDS": {
          "status": "UP",
          "details": {
            "database": "Informix Dynamic Server",
            "validationQuery": "select count(1) from systables",
            "result": 155
          }
        },
        "ayrDS": {
          "status": "UP",
          "details": {
            "database": "Informix Dynamic Server",
            "validationQuery": "select count(1) from systables",
            "result": 166
          }
        },
        "banffDS": {
          "status": "UP",
          "details": {
            "database": "Informix Dynamic Server",
            "validationQuery": "select count(1) from systables",
            "result": 156
          }
        },
        "caithnessDS": {
          "status": "UP",
          "details": {
            "database": "Informix Dynamic Server",
            "validationQuery": "select count(1) from systables",
            "result": 156
          }
        },
        "clackmannanDS": {
          "status": "UP",
          "details": {
            "database": "Informix Dynamic Server",
            "validationQuery": "select count(1) from systables",
            "result": 155
          }
        },
        "DS": {
          "status": "UP",
          "details": {
            "database": "H2",
            "validationQuery": "isValid()"
          }
        },
        "dumbartonDS": {
          "status": "UP",
          "details": {
            "database": "Informix Dynamic Server",
            "validationQuery": "select count(1) from systables",
            "result": 155
          }
        },
        "eastLothianDS": {
          "status": "UP",
          "details": {
            "database": "Informix Dynamic Server",
            "validationQuery": "select count(1) from systables",
            "result": 168
          }
        },
        "fifeDS": {
          "status": "UP",
          "details": {
            "database": "Informix Dynamic Server",
            "validationQuery": "select count(1) from systables",
            "result": 166
          }
        },
        "glasgowDS": {
          "status": "UP",
          "details": {
            "database": "Informix Dynamic Server",
            "validationQuery": "select count(1) from systables",
            "result": 171
          }
        },
        "invernessDS": {
          "status": "UP",
          "details": {
            "database": "Informix Dynamic Server",
            "validationQuery": "select count(1) from systables",
            "result": 166
          }
        },
        "kirkcudbrightDS": {
          "status": "UP",
          "details": {
            "database": "Informix Dynamic Server",
            "validationQuery": "select count(1) from systables",
            "result": 166
          }
        },
        "lanarkDS": {
          "status": "UP",
          "details": {
            "database": "Informix Dynamic Server",
            "validationQuery": "select count(1) from systables",
            "result": 159
          }
        },
        "midLothianDS": {
          "status": "UP",
          "details": {
            "database": "Informix Dynamic Server",
            "validationQuery": "select count(1) from systables",
            "result": 157
          }
        },
        "morayDS": {
          "status": "UP",
          "details": {
            "database": "Informix Dynamic Server",
            "validationQuery": "select count(1) from systables",
            "result": 156
          }
        },
        "orkneyDS": {
          "status": "UP",
          "details": {
            "database": "Informix Dynamic Server",
            "validationQuery": "select count(1) from systables",
            "result": 156
          }
        },
        "perthDS": {
          "status": "UP",
          "details": {
            "database": "Informix Dynamic Server",
            "validationQuery": "select count(1) from systables",
            "result": 169
          }
        },
        "renfrewDS": {
          "status": "UP",
          "details": {
            "database": "Informix Dynamic Server",
            "validationQuery": "select count(1) from systables",
            "result": 167
          }
        },
        "adesDS": {
          "status": "UP",
          "details": {
            "database": "Informix Dynamic Server",
            "validationQuery": "select count(1) from systables",
            "result": 156
          }
        },
        "stirlingDS": {
          "status": "UP",
          "details": {
            "database": "Informix Dynamic Server",
            "validationQuery": "select count(1) from systables",
            "result": 157
          }
        },
        "sutherlandDS": {
          "status": "UP",
          "details": {
            "database": "Informix Dynamic Server",
            "validationQuery": "select count(1) from systables",
            "result": 156
          }
        },
        "westLothianDS": {
          "status": "UP",
          "details": {
            "database": "Informix Dynamic Server",
            "validationQuery": "select count(1) from systables",
            "result": 155
          }
        }
      }
    },
    "discoveryComposite": {
      "description": "Discovery Client not initialized",
      "status": "UNKNOWN",
      "components": {
        "discoveryClient": {
          "description": "Discovery Client not initialized",
          "status": "UNKNOWN"
        }
      }
    },
    "diskSpace": {
      "status": "UP",
      "details": {
        "total": 128300593152,
        "free": 37138010112,
        "threshold": 10485760,
        "exists": true
      }
    },
    "livenessState": {
      "status": "UP"
    },
    "ping": {
      "status": "UP"
    },
    "rabbit": {
      "status": "UP",
      "details": {
        "version": "3.7.28"
      }
    },
    "readinessState": {
      "status": "UP"
    },
    "refreshScope": {
      "status": "UP"
    }
  },
  "groups": [
    "liveness",
    "readiness"
  ]
}
  

How would I be able to extract the key and get it based on this output

              app_custom,db=UP status_code=1
              app_custom,angleDS=UP status_code=1
              app_custom,argosleDS=UP status_code=1

CodePudding user response:

Your filter does not match up with the structure of your input. Your input has a recursive structure and your filter is only looking at the top level. The status is on the children of each component object so .value.components.status is incorrect.

I'm not sure what exactly your goal is but since this is recursive, you could use .. to descend through the tree and filter objects that have a components property and pick values to display.

$ curl -s ... | jq -r '
.. | .components? // empty | to_entries[]
    | [.key, .value.status] as [$id, $status]
    | "app_custom,\($id)=\($status) status_code=\(if $status=="UP" then 1 else 0 end)"
'
app_custom,db=UP status_code=1
app_custom,discoveryComposite=UNKNOWN status_code=0
app_custom,diskSpace=UP status_code=1
app_custom,livenessState=UP status_code=1
app_custom,ping=UP status_code=1
app_custom,rabbit=UP status_code=1
app_custom,readinessState=UP status_code=1
app_custom,refreshScope=UP status_code=1
app_custom,DS=UP status_code=1
app_custom,angusDS=UP status_code=1
app_custom,argyleDS=UP status_code=1
app_custom,ayrDS=UP status_code=1
app_custom,banffDS=UP status_code=1
app_custom,caithnessDS=UP status_code=1
app_custom,clackmannanDS=UP status_code=1
app_custom,dumbartonDS=UP status_code=1
app_custom,eastLothianDS=UP status_code=1
app_custom,fifeDS=UP status_code=1
app_custom,glasgowDS=UP status_code=1
app_custom,invernessDS=UP status_code=1
app_custom,kirkcudbrightDS=UP status_code=1
app_custom,lanarkDS=UP status_code=1
app_custom,midLothianDS=UP status_code=1
app_custom,morayDS=UP status_code=1
app_custom,orkneyDS=UP status_code=1
app_custom,perthDS=UP status_code=1
app_custom,renfrewDS=UP status_code=1
app_custom,adesDS=UP status_code=1
app_custom,stirlingDS=UP status_code=1
app_custom,sutherlandDS=UP status_code=1
app_custom,westLothianDS=UP status_code=1
app_custom,discoveryClient=UNKNOWN status_code=0

Here it is written a bit differently to hopefully illustrate what part is doing what.

..                                            # recurse through the object tree
  | .components? // empty                     # get the components object or skip if none
  | to_entries[]                              # convert the object to entries
  | [.key, .value.status] as [$id, $status]   # bind $id and $status variables
  | ($id | gsub("  "; "_")) as $fixedId       # replace spaces in id with underscores
  | (if $status=="UP" then 1 else 0 end) as $statusCode             # set statusCode to 1 if $status is "UP"
  | "app_custom,\($fixedId)=\($status) status_code=\($statusCode)"  # construct the final string given the components above
  • Related