当 Parse_Json 尝试解析“NaN”时,会给出 invalid 值。值是 pandas 数据帧中的空值
select
parse_json('{"fullName":"Niks",
"age":15,
"address": {
"areaCode":"1234",
},
"state": NaN ,
"movies": [
{"name":"Iron Man","budget":"$150M","producer":"Avi"},
{"name":"Sherlock Holmes","budget":"$200M","producer":"Joel Silver"},]}');
在解析雪花中具有 NaN 值的 JSON 时遇到问题。它说无效的 JSON,因此我们无法正确解析或使用 JSON 函数,尤其是在操作时。
目前,我们尝试了
x = 'NaN' then NULL else x end 的情况。
但这只是解决问题的临时方法。有没有其他方法/雪花功能可以帮助我。 ?
注意:我已尝试使用 try_parse_json,但问题仍然相同。
select
parse_json('{"fullName":"Niks",
"age":15,
"address": {
"areaCode":"1234",
},
"state": NaN ,
"movies": [
{"name":"Iron Man","budget":"$150M","producer":"Avi"},
{"name":"Sherlock Holmes","budget":"$200M","producer":"Joel Silver"},]}');
I face an issue when parsing JSON having NaN values in snowflake. it says invalid JSON and so we can not parse or use JSON function properly especially when we manipulate.
currently, we tried with the
case when x = 'NaN' then NULL else x end.
but that is a temporary way to solve it. is there any other way/ snowflake function that helps me out. ?
Note: I have tried with try_parse_json, but still the issue remains the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 JSON 数据无效,只需检查任何 JSON 验证器,它就会抛出错误,并且行为与屏幕截图中显示的行为相同。您的 json 文档有以下错误
以及修复逗号后的有效 json和 引号。
Your JSON data is invalid and just check any JSON validator , it will throw out error and it is same behaviour what is shown in your screenshot.Your json doc has below errors
and below valid json after fixing comma and quotes.
NaN 处理
NaN
是一个有效的双精度值,因此,如果您使用 TRY_TO_DOUBLE 解析它,您会得到正确的值:给出:
但如果您想替换它:
String replacement
您可以采用该字符串形式:
JSON NaN 处理
try_to_double 可以使用,技巧是 json 值是 varaint,并且 to_double 想要一个 TEXT/ STRING/VARCHAR 输入,因此您需要通过 ::text 将其转换为字符串
并且您可以通过直接比较来测试双精度值与 NaN,文档有关于这是如何的注释 与 IEEE 754 不同行为。
但 NULLIF 函数无法正确处理此问题,但您可以根据您的问题使用 IFF 而不是 CASE。
NaN handling
NaN
is a valid double value, thus if you parse it with TRY_TO_DOUBLE you get correct value:gives:
but if you are wanting to replace it:
String replacing
you can do that string form:
JSON NaN processing
try_to_double as can be used, the trick is json value are varaint, and to_double wants a TEXT/STRING/VARCHAR input, thus you need to cast it to string via ::text
And you can test for a double against NaN by comparing directly, the doc's have notes about how this is different to IEEE 754 behavior.
But the NULLIF function does not correctly handle this, but you can use IFF instead of CASE as per you question.