I have a string as given below in one of the columns in hive table.
{ABC:"ABCVAL",XYZ:"XYZVAL"}
I want to convert it to in HIVE
{"ABC":"ABCVAL","XYZ":"XYZVAL"}
How can I do this.
解决方案
Simple version
Assuming the value does not contain colon (:) symbol.
Look for sequence of characters other than {,: (key) followed by :
select regexp_replace('{ABC:"ABCVAL",XYZ:"XYZVAL"}','(?[^{,:]+):','"${key}":');
{"ABC":"ABCVAL","XYZ":"XYZVAL"}
Safe version
Look for optional { (beforekey), followed by a sequence of characters (key) which is immediately followed by : (colon), followed by ", sequence of characters (value) and then " again, followed by , or ] (aftervalue)
select regexp_replace
(
'{ABC:"ABCVAL",XYZ:"XYZVAL"}'
,'(?\\{)?(?.*?)(?:)(?".*?")(?[,}])'
,'${beforekey}"${key}"${colon}${value}${aftervalue}'
)
;
{"ABC":"ABCVAL","XYZ":"XYZVAL"}