Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-15-2025 08:43 PM
Hi @vgautam ,
You can make use of try_variant_get and variant_get to further differentiate between the two null scenarios.
Please find the below code for your reference.
%sql
WITH data AS (
SELECT parse_json('{"key1": "value", "key2": 123}') AS variant_col
)
SELECT
-- Check if the key 'key3' exists and its type
CASE
WHEN try_variant_get(variant_col, '$.key3', 'int') IS NOT NULL THEN 'Valid Cast'
WHEN try_variant_get(variant_col, '$.key3', 'string') IS NOT NULL THEN 'Type Mismatch'
ELSE 'Key Not Found'
END AS key3_existence,
-- Check if the key 'key1' exists and its type
CASE
WHEN try_variant_get(variant_col, '$.key1', 'int') IS NOT NULL THEN 'Valid Cast'
WHEN try_variant_get(variant_col, '$.key1', 'string') IS NOT NULL THEN 'Type Mismatch'
ELSE 'Key Not Found'
END AS key1_type_check
FROM data;