【问题标题】:MySql 8.0 - Filtered JSON queryMySql 8.0 - 过滤的 JSON 查询
【发布时间】:2021-06-22 12:12:41
【问题描述】:

我有一个 JSON blob,我正试图从评级信息部分下的邮政编码中提取一个值(预期值 = 90703)。 MySql 8 是否支持 JSON 过滤表达式?

JSON:

{
  "quote_number": null,
  "items": [
      {
          "annual_amount": 0.0,
          "pro_rata_amount": 0.0,
          "name": "Value Information",
          "categories": {
              "Use": "Single Family Detached",
              "Zip Code": "51431",
              "Floor Coverings": "Carpet"
          }
      },
      {
          "annual_amount": 0.0,
          "pro_rata_amount": 0.0,
          "name": "Rating Information",
          "categories": {
              "Number of Non-Weather Water Losses": "0",
              "Protection Class": "2",
              "Zip Code": "90703",
              "Special Hazard Interface Area": "N"
          }
      }
  ],
  "total": {
      "annual_fees": 0.0,
      "annual_premium": 9.0
  },
  "policy_id": null
}

路径:$.items[?(@.name=="Rating Information")].categories.Zip Code

当我通过此站点进行测试时获取数据时,路径似乎是正确的:https://jsonpath.com/

如果 MySql 不支持 JSON 过滤,建议的解决方法是什么?

【问题讨论】:

标签: mysql mysql-8.0 mysql-json


【解决方案1】:

MySQL 不完全支持 jsonpath 表达式。它不支持过滤表达式。此处记录了对 jsonpath 的有限支持:https://dev.mysql.com/doc/refman/8.0/en/json.html#json-path-syntax

我测试了你的数据:

set @j = '{ ...your json... }';

select * from json_table(@j, '$.items[*]' columns(
name text path '$.name',
zip_code text path '$.categories."Zip Code"'
)) as j;

+--------------------+----------+
| name               | zip_code |
+--------------------+----------+
| Value Information  | 51431    |
| Rating Information | 90703    |
+--------------------+----------+

然后,您可以在查询中添加 WHERE 子句以获得您想要的。

阅读有关 JSON_TABLE() 的更多信息:https://dev.mysql.com/doc/refman/8.0/en/json-table-functions.html

【讨论】:

    猜你喜欢
    • 2021-11-09
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 2017-12-15
    • 2016-05-22
    相关资源
    最近更新 更多