json数据
[
{
"spec": "70g/支",
"unit": "支",
"skuId": "1707823848651276346",
"amount": 6,
"weight": 70,
"spuName": "伊利 甄稀 苦咖啡味雪糕 流心冰淇淋 70g/支",
"weightUnit": "克(g)",
"weightForUnit": "70"
}
]
解析代码
SELECT
storeId store_code
,skuId sku_coding
,get_json_double(cast(t.unnest AS JSON), '$.amount') usage_amount
, get_json_string(cast(t.unnest AS JSON), '$.skuId') sub_commodity_SKU_coding
,t.unnest
,create_time
FROM zmaxx.zmaxx_data.api_commodity_combination , UNNEST(split(regexp_replace(regexp_replace(regexp_replace(childSkuList,' ','') , '\\]|\\[' ,'') ,'},{','}@&{' ),'@&')) AS t
代码解释
- regexp_replace(childSkuList,’ ‘,’‘):将空格替换,原因是数据中‘},{’,中间存在空格会导致下面代码无法解析
- regexp_replace(data, ‘\]|\[’ ,‘’):替换掉数组的中括号
- regexp_replace(data, ,‘},{’,‘}@&{’ ):多个对象之间的‘,’替换成特殊符号(不会出现在数据中),这个特殊符号用来split切分
- split(data,‘@&’):切分数据为数组
- UNNEST(array):类似hive中的explore()炸裂函数 和lateral view的结合,将一个数组拆成多行数据
- get_json_double(cast(t.unnest AS JSON), ‘$.amount’):根据key获取value
解析效果:
