简体   繁体   English

取消引用从嵌套 JSON 对象中提取的 JSON_EXTRACT 值

[英]Unquote JSON_EXTRACT value extracted from nested JSON object

I have a column with json type in MySQL database and the stored data structure is as follows:我在 MySQL 数据库中有一个json类型的列,存储的数据结构如下:

table row 1:
    {
       "products":[
          {
             "price_c":"800",
             "item":"cloth"
          },
          {
             "price_f":"100",
             "item":"food"
          }
       ]
    }
table row 2:
    {
       "products":[
          {
             "price_c":"600",
             "item":"cloth"
          },
          {
             "price_f":"200",
             "item":"food"
          }
       ]
    }

I have been trying to extract the price of ass "item":"food" and did that with the query:我一直在试图提取price驴的"item":"food" ,并做到了与查询:

SELECT column->>'$**.price_f' as Price FROM table
where JSON_CONTAINS(column, '{"item":"food"}', '$.products');

But the problem with this approach is that it returns但是这种方法的问题在于它返回

+---------+
|  Price  |
+---------+
| ["100"] |
| ["200"] |
+---------+

Although I have used ->> but still the result is not unquoted and I have figured out that it is probably due to the wildcard $**.price .虽然我使用了->>但结果仍然没有被引用,我发现这可能是由于通配符$**.price I have tried JSON_UNQUOTE and it also doesn't work.我试过JSON_UNQUOTE但它也不起作用。 How should I unquote the outputs?我应该如何取消引用输出?

Here is the sample project in dbfiddle这是dbfiddle 中示例项目

It is a string , so use string functions to remove the unwanted character它是一个字符串,因此使用字符串函数删除不需要的字符

SELECT REGEXP_REPLACE(col1->>'$**.price_f' ,'[^0-9]','') as Price FROM tab1 where JSON_CONTAINS(col1, '{"item":"food"}', '$.products');
\n| | Price |价格 |\n| | :---- | :---- |\n| | 200 | 200 |\n| | 100 | 100 |\n

db<>fiddle here db<> 在这里摆弄

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

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