HIVE gets json field specific value (single json or json array)

1. Get a specific value in a single json string

Function: get_json_object (single json, '$.Field to get')

Example:

Code: SELECT get_json_object('{"NAME":"Zhang San","ID":"1"}','$.NAME') as name;

SELECT get_json_object(‘{“NAME”:“张三”,“ID”:“1”}’,‘$.NAME’);
insert image description here
2. json_tuple

Syntax: json_tuple(json_string, k1, k2 ...)

Description: Parse the string json_string of json, specify multiple keys in json data, and return the corresponding value. Returns NULL if the input json string is invalid.

Example:

select  b.name ,b.age from tableName a lateral view json_tuple('{"name":"zhangsan","age":18}','name','age') b as name,age;

insert image description here
Note: Not in the above json_tuple function . If you add it when using the jsontuple function. If you add it when using the json_tuple function.If you are using j so ntWhen the u pl e function is added , the parsing will fail.
Hive parses the json array
1. Nested subqueries parse the json array

select good_id,get_json_object(sale_json,'$.monthSales') as monthSales from tableName  LATERAL VIEW explode(split(goods_id,','))goods as good_id  LATERAL VIEW explode(split(regexp_replace(regexp_replace(json_str , '\\[|\\]',''),'\\}\\,\\{','\\}\\;\\{'),'\\;')) sales as sale_json;
select t1.* from (
select
    t0.*
    ,get_json_object(tmp.task_per_step_definition, '$.daysOfMedication') as daysOfMedication
    ,get_json_object(tmp.task_per_step_definition, '$.dosageForm') as dosageForm
    ,get_json_object(tmp.task_per_step_definition, '$.drugCode') as drugCode
    ,get_json_object(tmp.task_per_step_definition, '$.frequency') as frequency
    ,get_json_object(tmp.task_per_step_definition, '$.frequencyName') as frequencyName
    ,get_json_object(tmp.task_per_step_definition, '$.groupNo') as groupNo
    ,get_json_object(tmp.task_per_step_definition, '$.medicineName') as medicineName
    ,get_json_object(tmp.task_per_step_definition, '$.price') as price
    ,get_json_object(tmp.task_per_step_definition, '$.remark') as remark
    ,get_json_object(tmp.task_per_step_definition, '$.route') as route
    ,get_json_object(tmp.task_per_step_definition, '$.routeName') as routeName
    ,get_json_object(tmp.task_per_step_definition, '$.singleDose') as singleDose
    ,get_json_object(tmp.task_per_step_definition, '$.singleDoseUnit') as singleDoseUnit
    ,get_json_object(tmp.task_per_step_definition, '$.specification') as specification
    ,get_json_object(tmp.task_per_step_definition, '$.totalDose') as totalDose
    ,get_json_object(tmp.task_per_step_definition, '$.totalDoseUnit') as totalDoseUnit
    ,get_json_object(tmp.task_per_step_definition, '$.unitPrice') as unitPrice
 from wedw_dwd.pg_med_risk_emr_western_medicine_prescription_df t0
lateral view explode(split(regexp_replace(regexp_replace(regexp_replace(t0.medicines,'^\\[\\{', '\\{'), '\\}\\]$', '\\}'), '\\}\\,\\{\\"nodeId\\"', '\\}\\@\\#\\{\\"nodeId\\"'), '\\@\\#')) tmp as task_per_step_definition
where t0.date_id = date_sub(current_date(),1) 
and t0.org_name like '%辛口%'
) t1

Guess you like

Origin blog.csdn.net/qq_44696532/article/details/130929998