使用 jq 到 return 对象中的特定信息 JSON

Using jq to return specific information in JSON object

我希望解析内部 JSON 对象的各个元素以在数据库中构建/加载。

下面是JSON对象。如何解析 id、name queue 等元素?我将循环迭代它并构建插入查询。

{
  "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                                                         
        }
      ]
  }

  }

获取具有值数组的元素非常简单。

 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);})

您最好将数据转换为数据库处理器容易使用的格式,例如 csv,然后再做一些处理。

$ jq -r '(.apps.app[0] | keys_unsorted) as $k
    | $k, (.apps.app[] | [.[$k[]]])
    | @csv
' input.json