Search code examples
jsonhadoophadoop-yarnjqmetrics

Using jq to return specific information in JSON object


I wish to parse individual elements of inner JSON object to build / load in the database.

The following is the JSON object. How can I parse elements like id, name queue etc? I will iterate it in loop and work and build the insert query.

{
  "apps": {
    "app": [
      {
        "id": "application_1540378900448_18838",
        "user": "hive",
        "name": "insert overwrite tabl...summary_view_stg_etl(Stage-2)",
        "queue": "Data_Ingestion",
        "state": "FINISHED",
        "finalStatus": "SUCCEEDED",
        "progress": 100
       }, 
       {
        "id": "application_1540378900448_18833",
        "user": "hive",
        "name": "insert into SNOW_WORK...metric_definitions')(Stage-13)",
        "queue": "Data_Ingestion",
        "state": "FINISHED",
        "finalStatus": "SUCCEEDED",
        "progress": 100                                                         
        }
      ]
  }

  }

Solution

  • its pretty simple just fetch elment which is having an array of values.

     var JSONOBJ={
          "apps": {
            "app": [
              {
                "id": "application_1540378900448_18838",
                "user": "hive",
                "name": "insert overwrite tabl...summary_view_stg_etl(Stage-2)",
                "queue": "Data_Ingestion",
                "state": "FINISHED",
                "finalStatus": "SUCCEEDED",
                "progress": 100
               }, 
               {
                "id": "application_1540378900448_18833",
                "user": "hive",
                "name": "insert into SNOW_WORK...metric_definitions')(Stage-13)",
                "queue": "Data_Ingestion",
                "state": "FINISHED",
                "finalStatus": "SUCCEEDED",
                "progress": 100                                                         
                }
              ]
          }
        
        }
        
        JSONOBJ.apps.app.forEach(function(o){console.log(o.id);console.log(o.user);console.log(o.name);})