hivesql解析json格式的key与value


json格式示例:
{“city_code”:“340100”,“county_code”:“340111”,“orientation”:“东”,“road_id”:35204271,“speed”:35.72}

解析json格式中的key

我们可以看到这个数据还是很规整的,首先先将这个json数据中的花括号:“{}”给去除,并将逗号:“,”也都给替换成冒号:“:”,双引号也给剔除,之后进行行列转换,选取需要的
1、剔除双引号、花括号
select translate(’{“city_code”:“340100”,“county_code”:“340111”,“orientation”:“东”,“road_id”:35204271,“speed”:35.72}’,’{}""’,’’)
在这里插入图片描述

2、逗号替换成冒号
select
regexp_replace(
translate(’{“city_code”:“340100”,“county_code”:“340111”,“orientation”:“东”,“road_id”:35204271,“speed”:35.72}’,’{}""’,’’),’,’,’:’
)
在这里插入图片描述

3、行列转换
select pos+1,val
from
(
select
regexp_replace(
translate(’{“city_code”:“340100”,“county_code”:“340111”,“orientation”:“东”,“road_id”:35204271,“speed”:35.72}’,’{}""’,’’),’,’,’:’
) str
) t lateral view posexplode(split(str,’:’)) t1 as pos,val
在这里插入图片描述
4、选取奇数行
select rn,val as key
from
(
select pos+1 as rn,val
from
(
select
regexp_replace(
translate(’{“city_code”:“340100”,“county_code”:“340111”,“orientation”:“东”,“road_id”:35204271,“speed”:35.72}’,’{}""’,’’),’,’,’:’
) str
) t lateral view posexplode(split(str,’:’)) t1 as pos,val
) t3
where rn%2=1
在这里插入图片描述

解析json格式中的value

1、get_json_object()函数
语法:get_json_object(json_string, ‘$.key’)
说明:解析json的字符串json_string,返回path指定的内容。如果输入的json字符串无效,那么返回NULL。这个函数每次只能返回一个数据项。

select
get_json_object(’{“city_code”:“340100”,“county_code”:“340111”,“orientation”:“东”,“road_id”:35204271,“speed”:35.72}’,’ . r o a d i d ′ ) , g e t j s o n o b j e c t ( ′ " c i t y c o d e " : " 340100 " , " c o u n t y c o d e " : " 340111 " , " o r i e n t a t i o n " : " 东 " , " r o a d i d " : 35204271 , " s p e e d " : 35.72 ′ , ′ .road_id'), get_json_object('{"city_code":"340100","county_code":"340111","orientation":"东","road_id":35204271,"speed":35.72}',' .roadid),getjsonobject("citycode":"340100","countycode":"340111","orientation":"","roadid":35204271,"speed":35.72,.speed’)

在这里插入图片描述
2、json_tuple()函数
在这里插入图片描述

感谢阅读,我是啊帅和和,一位大数据专业大四学生,祝你快乐。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啊帅和和。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值