json字符串解析之get_json_object與json_tuple


一、get_json_object

函數的作用:用來解析json字符串的一個字段:

  1. select get_json_object(flist,'$.filtertype') as filtertype
  2. ,get_json_object(flist, '$.filtersubtype')as filtersubtype
  3. ,get_json_object(flist, '$.filtername') as filtername
  4. from aaaaaaa

運行結果如下(只解析了一條數據):

 

對於jsonArray(json數組),如person表的xjson字段有數據:

[{"name":"王二狗","sex":"男","age":"25"},{"name":"李狗嗨","sex":"男","age":"47"}]

取出第一個json對象,那么hive sql為:

SELECT get_json_object(xjson,"$.[0]") FROM person;

 結果是:

{"name":"王二狗","sex":"男","age":"25"}

取出第一個json的age字段的值:

SELECT get_json_object(xjson,"$.[0].age") FROM person;

結果

25

二、json_tuple

函數的作用:用來解析json字符串中的多個字段

  1. select a.flist
  2. ,b.filtertype
  3. ,b.filtersubtype
  4. ,b.filtername
  5.   from aaaaaaaaa a
  6. lateral view json_tuple(flist,'filtertype', 'filtersubtype', 'filtername') b as
filtertype, filtersubtype,filtername; 

運行結果如下:




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM