在json中搜索精确的字符串值

w7t8yxp5  于 2021-06-25  发布在  Hive
关注(0)|答案(1)|浏览(324)

我有一个存储在json中的列
列名:s2s\U有效载荷
价值观:

{ 
    "checkoutdate":"2019-10-31",
    "checkindate":"2019-10-30",
    "numtravelers":"2",
    "domain":"www.travel.com.mx",
    "destination": {
                       "country":"MX",
                       "city":"Manzanillo"
                   },
    "eventtype":"search",
    "vertical":"hotels"
}

我希望查询数组中的精确值,而不是返回某个数据类型的所有值。我用json提取得到不同的计数。

SELECT 
    COUNT(JSON_EXTRACT(s2s_payload, '$.destination.code')) AS total, 
    JSON_EXTRACT(s2s_payload, '$.destination.code') AS destination
FROM 
    "db"."events_data_json5_temp"
WHERE 
    id = '111000'
    AND s2s_payload IS NOT NULL
    AND yr = '2019'
    AND mon = '10'
    AND dt >= '26'
    AND JSON_EXTRACT(s2s_payload, '$.destination.code')
GROUP BY  
    JSON_EXTRACT(s2s_payload, '$.destination.code')

如果我想筛选“eventtype”:“search”的位置,我该怎么做?
我尝试使用cast(s2s\u payload as char)='{“eventtype”“:”“search”}',但没有成功。

kx5bkwkv

kx5bkwkv1#

你需要使用 json_extract +a CAST 要获取要比较的实际值,请执行以下操作:

CAST(json_extract(s2s_payload, '$.eventtype') AS varchar) = 'search'

或者,和 json_extract_scalar (因此不需要 CAST ):

json_extract_scalar(s2s_payload, '$.eventtype')

相关问题