<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Differentiate null values in Variant Data type in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/differentiate-null-values-in-variant-data-type/m-p/104073#M41658</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/140097"&gt;@vgautam&lt;/a&gt;,&lt;/P&gt;
&lt;P class="_1t7bu9h1 paragraph"&gt;In the &lt;CODE&gt;try_variant_get&lt;/CODE&gt; function, NULL is returned in two scenarios:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;STRONG&gt;Object Not Found&lt;/STRONG&gt;: If the specified path does not exist in the JSON object.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Invalid Cast&lt;/STRONG&gt;: If the object at the specified path cannot be cast to the target type.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P class="_1t7bu9h1 paragraph"&gt;To differentiate between these two scenarios, you would need to handle the cases separately in your SQL logic. Unfortunately, the &lt;CODE&gt;try_variant_get&lt;/CODE&gt; function itself does not provide a direct way to distinguish between these two scenarios. You may need to use additional logic or checks to determine the exact cause of the NULL value&lt;/P&gt;</description>
    <pubDate>Fri, 03 Jan 2025 13:21:33 GMT</pubDate>
    <dc:creator>Alberto_Umana</dc:creator>
    <dc:date>2025-01-03T13:21:33Z</dc:date>
    <item>
      <title>Differentiate null values in Variant Data type</title>
      <link>https://community.databricks.com/t5/data-engineering/differentiate-null-values-in-variant-data-type/m-p/104071#M41656</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Based on the documentation &lt;A href="https://learn.microsoft.com/en-us/azure/databricks/sql/language-manual/functions/try_variant_get" target="_self"&gt;here&lt;/A&gt;, in both scenarios below try_variant_get returns a null:&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;SPAN&gt;If the object cannot be found&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;if the object cannot be cast&amp;nbsp;&lt;/SPAN&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;SPAN&gt;How does one differentiate between the two scenarios?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jan 2025 13:04:06 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/differentiate-null-values-in-variant-data-type/m-p/104071#M41656</guid>
      <dc:creator>vgautam</dc:creator>
      <dc:date>2025-01-03T13:04:06Z</dc:date>
    </item>
    <item>
      <title>Re: Differentiate null values in Variant Data type</title>
      <link>https://community.databricks.com/t5/data-engineering/differentiate-null-values-in-variant-data-type/m-p/104073#M41658</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/140097"&gt;@vgautam&lt;/a&gt;,&lt;/P&gt;
&lt;P class="_1t7bu9h1 paragraph"&gt;In the &lt;CODE&gt;try_variant_get&lt;/CODE&gt; function, NULL is returned in two scenarios:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;STRONG&gt;Object Not Found&lt;/STRONG&gt;: If the specified path does not exist in the JSON object.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Invalid Cast&lt;/STRONG&gt;: If the object at the specified path cannot be cast to the target type.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P class="_1t7bu9h1 paragraph"&gt;To differentiate between these two scenarios, you would need to handle the cases separately in your SQL logic. Unfortunately, the &lt;CODE&gt;try_variant_get&lt;/CODE&gt; function itself does not provide a direct way to distinguish between these two scenarios. You may need to use additional logic or checks to determine the exact cause of the NULL value&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jan 2025 13:21:33 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/differentiate-null-values-in-variant-data-type/m-p/104073#M41658</guid>
      <dc:creator>Alberto_Umana</dc:creator>
      <dc:date>2025-01-03T13:21:33Z</dc:date>
    </item>
    <item>
      <title>Re: Differentiate null values in Variant Data type</title>
      <link>https://community.databricks.com/t5/data-engineering/differentiate-null-values-in-variant-data-type/m-p/105828#M42276</link>
      <description>&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/106294"&gt;@Alberto_Umana&lt;/a&gt;&amp;nbsp;is it possible for you to elaborate on this please? What additional logic can be used to determine the exact cause? Could you please share an example?&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2025 03:58:50 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/differentiate-null-values-in-variant-data-type/m-p/105828#M42276</guid>
      <dc:creator>vgautam</dc:creator>
      <dc:date>2025-01-16T03:58:50Z</dc:date>
    </item>
    <item>
      <title>Re: Differentiate null values in Variant Data type</title>
      <link>https://community.databricks.com/t5/data-engineering/differentiate-null-values-in-variant-data-type/m-p/105835#M42281</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/140097"&gt;@vgautam&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;You can make use of &lt;STRONG&gt;try_variant_get&lt;/STRONG&gt; and &lt;STRONG&gt;variant_get&lt;/STRONG&gt; to further differentiate between the two null scenarios.&lt;/P&gt;&lt;P&gt;Please find the below code for your reference.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;%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;&lt;/LI-CODE&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2025 04:43:21 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/differentiate-null-values-in-variant-data-type/m-p/105835#M42281</guid>
      <dc:creator>Avinash_Narala</dc:creator>
      <dc:date>2025-01-16T04:43:21Z</dc:date>
    </item>
    <item>
      <title>Re: Differentiate null values in Variant Data type</title>
      <link>https://community.databricks.com/t5/data-engineering/differentiate-null-values-in-variant-data-type/m-p/105836#M42282</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/140097"&gt;@vgautam&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;Please ignore the above code and use below code&lt;/P&gt;&lt;LI-CODE lang="python"&gt;%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 variant_get(variant_col, '$.key3') IS NULL THEN 'Key Not Found'
    WHEN try_variant_get(variant_col, '$.key3', 'string') IS NULL THEN 'Type Mismatch'
    ELSE 'Valid Cast'
  END AS key3_existence,

  -- Check if the key 'key1' exists and its type
  CASE
    WHEN variant_get(variant_col, '$.key1') IS NULL THEN 'Key Not Found'
    WHEN try_variant_get(variant_col, '$.key1', 'string') IS NULL THEN 'Type Mismatch'
    ELSE 'Valid Cast'
  END AS key1_type_check
FROM data;&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 16 Jan 2025 05:09:41 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/differentiate-null-values-in-variant-data-type/m-p/105836#M42282</guid>
      <dc:creator>Avinash_Narala</dc:creator>
      <dc:date>2025-01-16T05:09:41Z</dc:date>
    </item>
  </channel>
</rss>

