使用jq在json对象中返回特定信息

ma8fv8wu  于 2021-06-01  发布在  Hadoop
关注(0)|答案(2)|浏览(404)

我希望解析内部json对象的各个元素以在数据库中构建/加载。
下面是json对象。如何解析id、name queue等元素?我将在循环中迭代它,并构建insert查询。

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

  }
4jb9z9bj

4jb9z9bj1#

它非常简单,只需获取具有一个值数组的elment。

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

06odsfpq2#

最好将数据转换成一种易于被数据库处理器使用的格式,比如csv,然后做点什么。

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

相关问题