【问题标题】:I have a MySQL database whereI am trying to retrieve JSON data that stores urls but the results keep coming back empty我有一个 MySQL 数据库,我试图在其中检索存储 url 的 JSON 数据,但结果一直为空
【发布时间】:2021-05-21 22:37:47
【问题描述】:

列中的数据如下所示:

{
"activity": {
    "token": "e7b64be4-74d4-7a6d-a74b-xxxxxxx",
    "route": "http://example.com/enroll/confirmation",
    "url_parameters": {
        "Success": "True",
        "ContractNumber": "003992314W",
        "Barcode": "1908Y10Z",
        "price": "8.99"
    },
    "server_info": {
        "cookie": [
            "_ga=xxxx; _fbp=xxx; _hjid=xxx; XDEBUG_SESSION=XDEBUG_ECLIPSE;"
        ],
        "upgrade-insecure-requests": [
            "1"
        ],
    },
    "campaign": "Unknown/None",
    "ip": "192.168.10.1",
    "entity": "App\\Models\\User",
    "entity_id": "1d9f3066-13ce-4659-b10d-xxxxx",
},
"time": "2021-05-21 20:15:02"

}

My code that I am using is below: 
SELECT *
FROM websote.stored_events
WHERE JSON_EXTRACT(event_properties, '$.route') = 'http://example.com/enroll/confirmation'
ORDER BY created_at DESC LIMIT 500;

该代码适用于其他 json 值而不是 url 值。我试过转义 MySQL 中的值,如下所示:

SELECT *
    FROM websote.stored_events
    WHERE JSON_EXTRACT(event_properties, '$.route') = 'http:///example.com//enroll//confirmation'
    ORDER BY created_at DESC LIMIT 500;

但仍然没有运气。对此的任何帮助将不胜感激!

【问题讨论】:

  • 我试过转义 - 转义使用\而不是/。无需逃避/

标签: mysql json escaping


【解决方案1】:

Route 是一个嵌套属性;我本来希望路径是

JSON_EXTRACT(event_properties, '$.activity.route')

【讨论】:

  • 同一级别的其他 json 名称可以在没有“活动”的情况下工作
  • 无论出于何种原因,转义都不起作用。 JSON_EXTRACT(event_properties, '$.token') 有效
  • 如果你select JSON_EXTRACT(event_properties, '$.route') from website.stored_events,你看到了什么?
  • '$.token' 有效 - 你确定吗? dbfiddle.uk/…
【解决方案2】:

您的示例数据不是有效的 JSON。对象或数组的最后一个元素后面不能有逗号:

    "entity_id": "1d9f3066-13ce-4659-b10d-xxxxx",
},
                                                ^ here

如果我删除了这种情况和其他类似情况,我可以测试你的数据插入到 JSON 列中,然后我可以提取你描述的对象元素:

mysql> select json_extract(event_properties, '$.activity.route') as route from stored_events;
+------------------------------------------+
| route                                    |
+------------------------------------------+
| "http://example.com/enroll/confirmation" |
+------------------------------------------+

请注意,该值是用双引号返回的。这是因为它作为 JSON 文档(标量字符串)返回。如果您想要原始值,则必须取消引用它:

mysql> select json_unquote(json_extract(event_properties, '$.activity.route')) as route from stored_events;
+----------------------------------------+
| route                                  |
+----------------------------------------+
| http://example.com/enroll/confirmation |
+----------------------------------------+

如果要搜索该值,则必须执行类似的表达式:

select * from stored_events 
where json_unquote(json_extract(event_properties, '$.activity.route')) 
  = 'http://example.com/enroll/confirmation'

基于存储在 JSON 中的对象属性进行搜索有缺点。

  • 它需要复杂的表达式,迫使您(以及您需要维护代码的任何其他人)了解有关 JSON 工作原理的大量细节。

  • 它不能用索引优化。此查询将运行表扫描。您可以添加带有索引的虚拟列,但这会增加复杂性,如果您需要 ALTER TABLE 来添加虚拟列,它就会错过 JSON 存储半结构化数据的要点。

底线是,如果您发现自己在查询的 WHERE 子句中使用 JSON 函数,则表明您应该将要搜索的列存储为普通列,而不是 JSON 文档的一部分。

然后,您可以编写易于阅读、易于同事维护、易于使用索引进行优化的代码:

SELECT * FROM stored_events 
WHERE route = 'http://example.com/enroll/confirmation';

您仍然可以在 JSON 文档中存储其他属性,但您希望可搜索的属性应存储在普通列中。

您可能想查看我的演示文稿How to Use JSON in MySQL Wrong。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 2013-07-17
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    相关资源
    最近更新 更多