类型一
原数据:bankid是array类型
目标数据:
{
"bankname": ["SPDB", "WS_HBBANK", "mytest"],
"grid": [{
"name": "阶段1",
"values": ["38.0,1.0,1.0"]
}, {
"name": "阶段2",
"values": ["11.0,0.0,0.0"]
}, {
"name": "阶段3",
"values": ["11.0,0.0,0.0"]
}]
}
代码:
SET odps.sql.hive.compatible = true
;
SELECT
TO_JSON(NAMED_STRUCT('bankname',bankid,'grid',grid)) AS json
FROM (
SELECT bankid
,COLLECT_LIST(grid) AS grid
FROM (
SELECT bankid
,grid
FROM (
SELECT bankid
,NAMED_STRUCT('name','阶段1','values',COLLECT_LIST(index_001)) AS index_001
,NAMED_STRUCT('name','阶段2','values',COLLECT_LIST(index_002)) AS index_002
,NAMED_STRUCT('name','阶段3','values',COLLECT_LIST(index_003)) AS index_003
FROM (
SELECT ARRAY('SPDB','WS_HBBANK','mytest') AS bankid
,'38.0,1.0,1.0' AS index_001
,'11.0,0.0,0.0' AS index_002
,'11.0,0.0,0.0' AS index_003
) o1
GROUP BY bankid
) o2
LATERAL VIEW EXPLODE(ARRAY(index_001,index_002,index_003)) tmp AS grid
) o3
GROUP BY bankid
) o4
;
类型二
原数据:
目标数据:
{
"id": 53830,
"value": [{
"下装款式": ["直筒裤"]
}, {
"下装设计": ["纯色"]
}, {
"厚度": ["正常"]
}, {
"版型": ["正常"]
}, {
"穿搭风格": ["机能", "休闲"]
}, {
"裤长": ["长裤"]
}]
}
代码:
SELECT CONCAT('{"id":',id,',"value":',result,'}') AS json
FROM (
SELECT id
,CONCAT('[',CONCAT_WS(',',COLLECT_LIST(CONCAT('{"',type,'":',value,'}'))),']') AS result
FROM (
SELECT id
,type
,CONCAT('[',CONCAT_WS(',',COLLECT_LIST(CONCAT('"',value,'"'))),']') AS value
FROM temp_wxt_test_0627
GROUP BY id
,type
) o1
GROUP BY id
) o2
;