2

I have a json column day_data which has data in json format. How do I achieve the expected output using Hive?

input:

{"_id":"1","name":"abc","attribs":[{"minutes":0,"name":"sedentary"},{"minutes":0,"name":"lightly"},{"minutes":0,"name":"fairly"},{"minutes":28,"name":"very"}],"validated":true}

output: id name attrib_minutes attrib_name validated 1 abc 0 sedentary true 1 abc 0 lightly true 1 abc 0 fairly true 1 abc 28 very true

I am able to extract id, name and validated fields using get_json_object command, select get_json_object(day_data,'$._id') as id, get_json_object(day_data,'$.name') as name, get_json_object(day_data,'$.validated') as validated from temp_table;

How do I extract the nested json attributes (attrib_minutes and attrib_name)?

0

1 Answer 1

3
select  j.id
       ,j.name
       ,get_json_object  (day_data,concat('$.attribs[',e.i,'].minutes'))    as attrib_minutes
       ,get_json_object  (day_data,concat('$.attribs[',e.i,'].name'))       as attrib_name
       ,j.validated

from                    temp_table t
        lateral view    json_tuple  (day_data,'_id','name','validated')  j as id,name,validated
        lateral view    posexplode  (split(get_json_object (day_data,'$.attribs[*].name'),'","')) e as i,x
;

j.id    j.name  attrib_minutes  attrib_name j.validated
1   abc 0   sedentary   true
1   abc 0   lightly true
1   abc 0   fairly  true
1   abc 28  very    true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.