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..c53a764d 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,17 @@ public String asJsonString() { } } + /** + * Converts the variant as valid JsonNode. + * + * @return A valid json Node + * @since 0.8.0 + */ + public JsonNode asJsonNode() { + verify(VariantTypes.String); + return objectToJsonNode(value); + } + /** * Converts the variant as binary value. * diff --git a/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java b/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java index aa9fb117..8f31a004 100644 --- a/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java +++ b/src/test/java/com/snowflake/snowpark_test/JavaVariantSuite.java @@ -1,5 +1,6 @@ package com.snowflake.snowpark_test; +import com.fasterxml.jackson.databind.JsonNode; import com.snowflake.snowpark_java.types.Geography; import com.snowflake.snowpark_java.types.InternalUtils; import com.snowflake.snowpark_java.types.Variant; @@ -365,4 +366,17 @@ public void equalsAndToString() { assert v1.toString().equals("123"); } + + @Test + public void javaJsonNodeVariantConverter() throws IllegalArgumentException { + // Scala variant to Java Variant + com.snowflake.snowpark.types.Variant sv = + new com.snowflake.snowpark.types.Variant( + "{\"name\": \"abc\",\"grade\": 2, \"Interest\": \"cricket\"}"); + Variant jResult = InternalUtils.createVariant(sv); + JsonNode jsonNode = jResult.asJsonNode(); + + assert jsonNode.get("name").asText().equals("abc"); + assert jsonNode.get("grade").asInt() == 2; + } }