简体   繁体   English

如何从 SQL 中提取 JSON 值,其中列值有时为空

[英]How to extract JSON value from SQL where where column values sometimes empty

I have a table with column 'sometimesjson' that somtimes contains a value like this:我有一个包含“sometimesjson”列的表,其中有时包含这样的值:

[{"id":111,"nam":"PROVIDER_this_one","qty":null}]

and sometimes the field completely is empty, not just "[]" but completely empty/null field.有时该字段完全为空,不仅仅是“[]”,而是完全为空/空字段。

So I try to return "PROVIDER_this_one" with this, but errors...所以我尝试用这个返回“PROVIDER_this_one”,但是错误......

CASE WHEN JSON_VALID(sometimesjson) 
            THEN 
            JSON_EXTRACT(sometimesjson, '$.nam') 
            AS 'FROMJSON' 
             ELSE null  

Any ideas?有任何想法吗?

I figured it out.我想到了。 I guess because the json value actually is an array, the correct way for me to call "nam" for every value in that table field value array was this... '$[*].nam' instead of '$.nam' it's working now with...我猜是因为 json 值实际上是一个数组,所以我为该表字段值数组中的每个值调用“nam”的正确方法是...... '$ [*] .nam'而不是'$ .nam'它现在正在与...一起工作

    CASE WHEN JSON_VALID(sometimesjson) 
            THEN 
            JSON_EXTRACT(sometimesjson, '$[*].nam') 
            AS 'FROMJSON' 
             ELSE null  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM