[
https://issues.apache.org/jira/browse/CALCITE-4989?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17497503#comment-17497503
]
Gavin Ray commented on CALCITE-4989:
------------------------------------
Okay, I fixed it :). I changed:
{code:java}
public static String jsonize(@Nullable Object input) {
return JSON_PATH_JSON_PROVIDER.toJson(input);
}
{code}
To the below.
The issue was just that Jackson was interpreting each level of JSON object as a
string, so it was escaping it to be a valid JSON string value.
So for each value in a JSON object, if it parses as a valid JSON object itself,
we convert it to a Map and move on + skip calling "toJson()".
That way, the whole object only gets one top-level parse call after everything
has been visited:
{code:java}
public static String jsonize(@Nullable Object input) {
if (input instanceof Map) {
Map<String, Object> map = (Map<String, Object>) input;
// For each key, if the value is a valid JSON object, then parse to Map
for (Map.Entry<String, Object> entry : map.entrySet()) {
String valueStr = entry.getValue().toString();
try {
JsonNode node =
JSON_PATH_JSON_PROVIDER.getObjectMapper().readTree(valueStr);
if (node.isObject()) {
entry.setValue(node);
} else {
entry.setValue(valueStr);
}
} catch (JsonProcessingException e) {
// Ignore
}
}
}
return JSON_PATH_JSON_PROVIDER.toJson(input);
}
{code}
> Nested JSON_OBJECT creation does not produce proper json
> --------------------------------------------------------
>
> Key: CALCITE-4989
> URL: https://issues.apache.org/jira/browse/CALCITE-4989
> Project: Calcite
> Issue Type: Bug
> Components: core
> Affects Versions: 1.29.0
> Reporter: Mans Singh
> Priority: Critical
> Labels: json_object
>
> I am trying to create a nested json object using JSON_OBJECT and am getting a
> json with escaped quotes.
>
> I have the following query in sql line :
>
> {code:java}
> select JSON_OBJECT(
> KEY 'level1'
> VALUE(
> JSON_OBJECT(
> KEY 'level2'
> VALUE(
> JSON_OBJECT(
> KEY 'level3'
> VALUE 'val3')
> )
> )
> )
> )
> from (values ('{"a":{"b":2}}')) t(v);
>
> {code}
> And it produces the result:
>
> {noformat}
> -------------------------------------------------------------
> EXPR$0
> -------------------------------------------------------------
> {"level1":" {\"level2\":\"{\\\"level3\\\":\\\"val3\\\"}\"}"}
> -------------------------------------------------------------
> {noformat}
>
> I was expecting the result as follows (without quote escapes):
>
> {noformat}
> {"level1":{"level2":{"level3":"val3"}}}
>
> {noformat}
> Also, see examples created by Stamatis
> -
> [https://github.com/zabetak/calcite/commit/988c13ce1ff551d6e4046a3c027ff298f79971f8]
--
This message was sent by Atlassian Jira
(v8.20.1#820001)