【MaxCompute】MaxCompute 处理复杂json、json数组

本文深入探讨了如何使用GET_JSON_OBJECT函数从复杂的JSON数据中抽取特定字段,通过具体示例展示了处理JSON数组及嵌套JSON串的技巧。

json数组

数据样例:

[{"date":"2016-09-24","period":1,"custom_dimension":"","term_type_id":14}]

GET_JSON_OBJECT 函数:

SELECT 
    GET_JSON_OBJECT(name_tmp,'$.date')
    FROM (
        SELECT 
            regexp_replace(regexp_replace(regexp_replace(json_str,'^\\[',''),'\\]$',''),'},\\{','}|{') as json_str1
        from (
            select '[{"date":"2016-09-24","period":1,"custom_dimension":"","term_type_id":14}]' as json_str 
        ) t1
    ) t2
    lateral view explode(split(json_str1,'\\|')) b AS name_tmp
;

复杂json串

数据样例:

{"approved":[{"code":2,"receiver_type":12,"title_params":[1],"content_params":[1,3,2],"type":1}],"remind":[{"code":3,"receiver_type":6,"title_params":[],"content_params":[1,3,2],"type":1}]}

GET_JSON_OBJECT 函数:

SELECT 
    GET_JSON_OBJECT(name_tmps,'$.title_params')
    FROM (
        SELECT 
            regexp_replace(regexp_replace(regexp_replace(json_str,'^\\[',''),'\\]$',''),'},\\{','}|{') as json_str1
        from (
            select 
            (
                SELECT 
                    GET_JSON_OBJECT(name_tmp,'$.remind')
                FROM (
                SELECT '{"approved":[{"code":2,"receiver_type":12,"title_params":[1],"content_params":[1,3,2],"type":1}],"remind":[{"code":3,"receiver_type":6,"title_params":[],"content_params":[1,3,2],"type":1}]}' AS name_tmp
            ) )
             as json_str 
        ) t1
    ) t2
    lateral view explode(split(json_str1,'\\|')) b AS name_tmps
;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值