[ https://issues.apache.org/jira/browse/SPARK-34993?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17319958#comment-17319958 ]
Kousuke Saruta edited comment on SPARK-34993 at 4/13/21, 7:14 AM: ------------------------------------------------------------------ Hi [~laurikoobas]. I think all of the results is correct. About the following two expressions, {code:java} from_json('{"msg":"\\"}', schema_of_json(to_json(named_struct('msg', '\\')))) from_json('{"msg":"\\"}', 'msg string') {code} {"msg": "\\"} is just a string literal. So, if you need to give a json formed string which contains escaped characters like, {"msg": "\\"} you should write {"msg": "\\\\"} In this case, those expression should be: {code:java} from_json('{"msg":"\\\\"}', schema_of_json(to_json(named_struct('msg', '\\')))) from_json('{"msg":"\\\\"}', 'msg string') {code} About thefollowing expression, {code:java} from_json(to_json(named_struct('msg', '\\')), schema_of_json(to_json(named_struct('msg', '\\')))) {code} The evaluated result is {"msg":"\"} in my Spark 3.1.1 environment. It seems to be different from {"msg":"\\"} you get but it should be {"msg": "\"} was (Author: sarutak): Hi [~laurikoobas]. I think all of the results is correct. About the following two expressions, {code:java} from_json('{"msg":"\\"}', schema_of_json(to_json(named_struct('msg', '\\')))) from_json('{"msg":"\\"}', 'msg string') {code} '\{"msg": "\\"}' is just a string literal and "\\" is regarded as "\". So, if you need to give an escaped character like "\\", you should write "\\\\". In this case, those expression should be: {code:java} from_json('{"msg":"\\\\"}', schema_of_json(to_json(named_struct('msg', '\\')))) from_json('{"msg":"\\\\"}', 'msg string') {code} About thefollowing expression, {code:java} from_json(to_json(named_struct('msg', '\\')), schema_of_json(to_json(named_struct('msg', '\\')))) {code} The evaluated result is \{"msg":"\"} in my Spark 3.1.1 environment. It seems to be different from the result you get (\{"msg":"\\"}) but it should be \{"msg": "\"}. > from_json() acts differently on created and literal strings with backslashes > ---------------------------------------------------------------------------- > > Key: SPARK-34993 > URL: https://issues.apache.org/jira/browse/SPARK-34993 > Project: Spark > Issue Type: Bug > Components: SQL > Affects Versions: 3.1.1 > Environment: Databricks DBR 8.1 > Reporter: Lauri Koobas > Priority: Major > Attachments: image-2021-04-11-07-21-02-750.png > > > JSON string with the value that contains backslashes fails to be recovered by > `from_json()`. > I found that if the same string is created with `to_json(named_struct())` > then it actually does work. > > The following code to reproduce. I would expect all of these methods to > return the same (correct) result: > {code:java} > select to_json(named_struct('msg', '\\')) > , schema_of_json(to_json(named_struct('msg', '\\'))) > , from_json(to_json(named_struct('msg', '\\')), > schema_of_json(to_json(named_struct('msg', '\\')))) > , from_json('{"msg":"\\"}', schema_of_json(to_json(named_struct('msg', > '\\')))) > , from_json('{"msg":"\\"}', 'msg string') > > {code} > > !image-2021-04-11-07-21-02-750.png! -- This message was sent by Atlassian Jira (v8.3.4#803005) --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@spark.apache.org For additional commands, e-mail: issues-h...@spark.apache.org