0

我希望解析内部 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                                                         
        }
      ]
  }

  }
4

2 回答 2

1

您最好将数据转换为数据库处理器易于使用的格式,例如 csv,然后对其进行处理。

$ jq -r '(.apps.app[0] | keys_unsorted) as $k
    | $k, (.apps.app[] | [.[$k[]]])
    | @csv
' input.json
于 2018-11-30T21:39:47.553 回答
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);})

于 2018-11-30T13:10:03.310 回答