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

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

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

  1. {
  2. "apps": {
  3. "app": [
  4. {
  5. "id": "application_1540378900448_18838",
  6. "user": "hive",
  7. "name": "insert overwrite tabl...summary_view_stg_etl(Stage-2)",
  8. "queue": "Data_Ingestion",
  9. "state": "FINISHED",
  10. "finalStatus": "SUCCEEDED",
  11. "progress": 100
  12. },
  13. {
  14. "id": "application_1540378900448_18833",
  15. "user": "hive",
  16. "name": "insert into SNOW_WORK...metric_definitions')(Stage-13)",
  17. "queue": "Data_Ingestion",
  18. "state": "FINISHED",
  19. "finalStatus": "SUCCEEDED",
  20. "progress": 100
  21. }
  22. ]
  23. }
  24. }
4jb9z9bj

4jb9z9bj1#

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

  1. var JSONOBJ={
  2. "apps": {
  3. "app": [
  4. {
  5. "id": "application_1540378900448_18838",
  6. "user": "hive",
  7. "name": "insert overwrite tabl...summary_view_stg_etl(Stage-2)",
  8. "queue": "Data_Ingestion",
  9. "state": "FINISHED",
  10. "finalStatus": "SUCCEEDED",
  11. "progress": 100
  12. },
  13. {
  14. "id": "application_1540378900448_18833",
  15. "user": "hive",
  16. "name": "insert into SNOW_WORK...metric_definitions')(Stage-13)",
  17. "queue": "Data_Ingestion",
  18. "state": "FINISHED",
  19. "finalStatus": "SUCCEEDED",
  20. "progress": 100
  21. }
  22. ]
  23. }
  24. }
  25. JSONOBJ.apps.app.forEach(function(o){console.log(o.id);console.log(o.user);console.log(o.name);})
展开查看全部
06odsfpq

06odsfpq2#

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

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

相关问题