sql解析嵌套json+数组

Posted 甜_tian

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql解析嵌套json+数组相关的知识,希望对你有一定的参考价值。

sql解析json的几种情况汇总如下:

目录

sql解析json的几种情况汇总如下:

普通json

json嵌套数组,数组中包含json

json嵌套数组,数组中嵌套复杂json


基础信息:

表名:table

字段名:t_json

普通json


    "name": "a",
    "title":"hh"

sql:

select get_json_object(t_json, '$.name') as name
      ,get_json_object(t_json, '$.title') as title
from table;

json嵌套数组,数组中包含json

        people_list:[

               

                        "people_name": "b",

                        "people_age":18

                ,

                

                        "people_name": "b",

                        "people_age":18

                

        ]

sql:

select get_json_object(people, '$.people_name') AS people_name
       ,get_json_object(people, '$.people_age') AS people_age
from (
    select get_json_object(t_json, '$.people_list') AS people_list
    from table
)
LATERAL VIEW explode(split(regexp_replace(regexp_replace(people_list,'\\\\[|\\\\]',''),',',';'),';')) num AS people
;

//注释
//数组类型的数据,需要做一行转多行的操作,用到LATERAL VIEW
//先看最内层regexp_replace(people_list,'\\\\[|\\\\]','') 这步操作是把数组外面的[]去掉

//regexp_replace(regexp_replace(people_list,'\\\\[|\\\\]',''),',',';') 
//这一步是把数组中每个json连接的地方,由 , 替换成 ;

//最后根据替换后的;去split每个元素

json嵌套数组,数组中嵌套复杂json

        "

":[

               

                        "id": 20,

                         "package_name":"1",

                        "package_info":

                                package_list:[

                                        "aaa",

                                         "bbb"

                                ]

                        

                ,

                

                        "id": 21,

                         "package_name":"2",

                        "package_info":

                                package_list:[

                                        "ccc",

                                         "ddd"

                                ]

                        

                

        ]

sql:

select id, package_name, packages
from table
LATERAL VIEW json_tuple(t_json,"people_package[*].id") num AS ids
LATERAL VIEW explode(split(regexp_replace(ids,'\\\\[|\\\\]',''),',')) num AS id
LATERAL VIEW json_tuple(t_json,"people_package[*].package_name") num AS package_names
LATERAL VIEW explode(split(regexp_replace(package_names,'\\\\[|\\\\]',''),',')) num AS package_name
LATERAL VIEW json_tuple(t_json,"people_package[*].package_info[*].package_list") num AS packages
LATERAL VIEW explode(split(regexp_replace(packages,'\\\\[|\\\\]',''),',')) num AS package
;


//注释

//id
//LATERAL VIEW json_tuple(t_json,"people_package[*].id") num AS ids
//把people_package数组下的id列表获取出来,ids就是所有id的汇总
//LATERAL VIEW explode(split(regexp_replace(ids,'\\\\[|\\\\]',''),',')) num AS id
//把ids两边的[]去掉,根据','去分割,利用LATERAL VIEW一行变为多行

//package_name 思路如上

//package 步骤一致,只是多取了一层

以上是关于sql解析嵌套json+数组的主要内容,如果未能解决你的问题,请参考以下文章

sql解析嵌套json+数组

SQL:LATERAL VIEW函数解析多嵌套的json

json数组嵌套数组

使用 JQuery 解析 JSON 嵌套数组

使用 Dart 语言解析嵌套 JSON 数组并将其放入模型类中

在解析的 JSON 嵌套数组上运行 foreach