使用jq返回JSON对象中的特定信息

时间:2018-11-30 12:52:28

标签: json hadoop yarn jq metrics

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

以下是JSON对象。如何解析ID,名称队列等元素?我将在循环中对其进行迭代,并工作并构建插入查询。

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

  }

2 个答案:

答案 0 :(得分:1)

最好将数据转换为数据库处理器可以轻松使用的格式(例如csv),然后对其进行处理。

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

答案 1 :(得分:0)

它非常简单,只需获取具有一组值的元素即可。

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