diff --git a/src/main/java/com/snowflake/snowpark_java/types/Variant.java b/src/main/java/com/snowflake/snowpark_java/types/Variant.java index 469ec8e6..140699d2 100644 --- a/src/main/java/com/snowflake/snowpark_java/types/Variant.java +++ b/src/main/java/com/snowflake/snowpark_java/types/Variant.java @@ -376,6 +376,26 @@ public String asJsonString() { } } + /** + * Return the variant value as a JsonNode. This function allows to read the JSON object directly + * as JsonNode from variant column rather parsing it as String + * + *
{@code - to get the first value from array for key "a" + * + * Variant jv = new Variant("{\"a\": [1, 2], \"b\": \"c\"}"); + * System.out.println(jv.asJsonNode().get("a").get(0)); + * + * output + * 1 + * }+ * + * @return A valid JsonNode + * @since 1.14.0 + */ + public JsonNode asJsonNode() { + return value; + } + /** * Converts the variant as binary value. * diff --git a/src/main/scala/com/snowflake/snowpark/types/Variant.scala b/src/main/scala/com/snowflake/snowpark/types/Variant.scala index 5ff86f9c..be0424b4 100644 --- a/src/main/scala/com/snowflake/snowpark/types/Variant.scala +++ b/src/main/scala/com/snowflake/snowpark/types/Variant.scala @@ -381,6 +381,23 @@ class Variant private[snowpark] ( } } + /** + * Return the variant value as a JsonNode. This function allows to read the JSON object directly + * as JsonNode from variant column rather parsing it as String + * Example - to get the first value from array for key "a" + * {{{ + * val sv = new Variant("{\"a\": [1, 2], \"b\": 3, \"c\": \"xyz\"}") + * println(sv.asJsonNode().get("a").get(0)) + * output + * 1 + * }}} + * + * @since 1.14.0 + */ + def asJsonNode(): JsonNode = { + value + } + /** * Converts the variant as binary value * @since 0.2.0 diff --git a/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java b/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java index aa9fb117..83ce4ea8 100644 --- a/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java +++ b/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java @@ -365,4 +365,11 @@ public void equalsAndToString() { assert v1.toString().equals("123"); } + + @Test + public void javaJsonNodeVariantConverter() throws IllegalArgumentException { + Variant jv = new Variant("{\"a\": [1, 2], \"b\": \"c\"}"); + assert (jv.asJsonNode().get("a").isArray()); + assert (jv.asJsonNode().get("b").asText().equals("c")); + } } diff --git a/src/test/scala/com/snowflake/snowpark_test/ScalaVariantSuite.scala b/src/test/scala/com/snowflake/snowpark_test/ScalaVariantSuite.scala index 35e8c572..1f03c3f6 100644 --- a/src/test/scala/com/snowflake/snowpark_test/ScalaVariantSuite.scala +++ b/src/test/scala/com/snowflake/snowpark_test/ScalaVariantSuite.scala @@ -311,4 +311,11 @@ class ScalaVariantSuite extends FunSuite { assert(v1.toString() == "123") } + + test("JsonNode") { + val sv = new Variant("{\"a\": [1, 2], \"b\": 3, \"c\": \"xyz\"}") + assert(sv.asJsonNode().get("a").isArray) + assert(sv.asJsonNode().get("b").asInt().equals(3)) + assert(sv.asJsonNode().get("c").asText().equals("xyz")) + } }