hive解析json格式

{
    "store": {
        "fruit": [
            {
                "weight": 8, 
                "type": "apple"
            }, 
            {
                "weight": 9, 
                "type": "pear"
            }
        ], 
        "bicycle": {
            "price": 19.95, 
            "color": "red"
        }
    }, 
    "email": "amy@only_for_json_udf_test.net", 
    "owner": "amy"
}

方式1:采用get_json_object

例如上面数据为str

则:select get_json_object(str,'$.owner'); 结果为amy

select get_json_object(str,'$.store.fruit[0].weight'); 结果为8

方式2:采用json_tuple,可以返回多个值

select json_tuple('{"store":
{"fruit":[{"weight":8,"type":"apple"},{"weight":9,"type":"pear"}],
bicycle":"price":19.951,"color":"red1"}},"email":"amy@only_for_json_udf_test.net","owner":"amy1"}',
 'store.fruit[0].weight','owner','store');
+-------+-------+----------------------------------------------------+
|  c0   |  c1   |                         c2                         |
+-------+-------+----------------------------------------------------+
| NULL  | amy1  | {"fruit":[{"weight":8,"type":"apple"},{"weight":9,"type":"pear"}],"bicycle":{"price":19.951,"color":"red1"}} |

+-------+-------+----------------------------------------------------+

多层查询没有结果,根查询可以使用。

视具体情况选用




你可能感兴趣的:(hive)