Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snow-802269 Add JsonNode function to read variant column as json object #149

Merged
merged 15 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/main/java/com/snowflake/snowpark_java/types/Variant.java
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,27 @@ public String asJsonString() {
}
}

/**
* Converts the variant as valid JsonNode. This function allows to read the JSON object directly
* rather parsing it as String and then extract the desired value
*
* <pre>{@code - to get the first value from array for key "a"
*
* Variant jv = new Variant("{\"a\": [1, 2], \"b\": \"c\"}");
* JsonNode jNode = jv.asJsonNode();
* System.out.println(jNode.get("a").get(0));
*
* output
* 1
* }</pre>
*
* @return A valid JsonNode
* @since 1.14.0
*/
public JsonNode asJsonNode() {
return objectToJsonNode(value);
sfc-gh-gmahadevan marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Converts the variant as binary value.
*
Expand Down
17 changes: 17 additions & 0 deletions src/main/scala/com/snowflake/snowpark/types/Variant.scala
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,23 @@ class Variant private[snowpark] (
}
}

/**
* Converts the variant as valid JsonNode
* This function allows to read the JSON object directly rather parsing it as String
* and then extract the desired value
* Example - to get the first value from array for key "a"
* {{{
* val sv = new Variant("{\"a\": [1, 2], \"b\": 3, \"c\": \"xyz\"}")
* println(sv.asMap.get("a").get.asJsonNode().get(0))
* output
* 1
* }}}
* @since 1.14.0
*/
def asJsonNode(): JsonNode = {
objectToJsonNode(value)
}

/**
* Converts the variant as binary value
* @since 0.2.0
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -365,4 +366,12 @@ public void equalsAndToString() {

assert v1.toString().equals("123");
}

@Test
public void javaJsonNodeVariantConverter() throws IllegalArgumentException {
Variant jv = new Variant("{\"a\": [1, 2], \"b\": \"c\"}");
JsonNode jNode = jv.asJsonNode();
assert (jNode.get("a").isArray());
assert (jNode.get("b").asText().equals("c"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.asMap.get("a").get.asJsonNode().isArray)
assert(sv.asMap.get("b").get.asJsonNode().asInt().equals(3))
assert(sv.asMap.get("c").get.asJsonNode().asText().equals("xyz"))
}
}
Loading