AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 335969
Accepted
Gea-Suan Lin
Gea-Suan Lin
Asked: 2024-02-18 04:40:12 +0800 CST2024-02-18 04:40:12 +0800 CST 2024-02-18 04:40:12 +0800 CST

MySQL 查询的 JSON 输出中未转义引号的问题

  • 772

我遇到了一个问题,MySQL 查询的 JSON 输出中的引号没有被转义。我执行了以下命令从 WordPress 数据库中提取数据并将其保存到文件中:

echo "SELECT JSON_OBJECT('id', ID, 'title', post_title, 'body', post_content) FROM wp_posts LIMIT 1;" | mysql -u root -p blog_gslin_org > blog.output

输入密码并检查 的内容后blog.output,我注意到 JSON 值内的引号没有转义,这可能会导致其他应用程序使用 JSON 时出现解析错误。这是输出的片段:

{"id": 2, "title": "關於我 (about me)", "body": "這個 Blog 主要是偏技術方面的資訊 (以及各種雜七雜八的抱怨文章),另外有幾個 Blog 是其他方面的:\\r\\n\\r\\n<ul>\\r\\n\\t<li><a href=\\"http://blog.gslin.info/\\" rel=\\"tag\\">blog.gslin.info</a>:<del datetime=\\"2007-07-14T09:49:18+00:00\\">跟課業有關的 (包括實驗室研究的東西)。</del>改放 ACG 相關的資訊。</li>\\r\\n\\t<li><a href=\\"http://blog.gslin.net/\\" rel=\\"tag\\">blog.gslin.net</a>:跟網路有關的。</li>\\r\\n</ul>\\r\\n\\r\\n除了 Blog 外,你可以在這些地方找到我:\\r\\n\\r\\n<ul>\\r\\n\\t<li><a href=\\"https://abpe.org/@gslin\\">Mastodon</a></li>\\r\\n\\t<li><a href=\\"https://twitter.com/gslin\\">Twitter</a></li>\\r\\n\\t<li><a href=\\"https://www.facebook.com/gslin\\">Facebook</a></li>\\r\\n\\t<li><a href=\\"https://www.plurk.com/gslin\\">Plurk</a></li>\\r\\n\\t<li><a href=\\"https://www.instagram.com/gslin\\">Instagram</a></li>\\r\\n\\t<li><a href=\\"https://www.flickr.com/photos/gslin\\">Flickr</a></li>\\r\\n\\t<li><a href=\\"https://www.linkedin.com/in/gslin/\\">Linkedin</a></li>\\r\\n</ul>\\r\\n\\r\\n關於我的連絡的方法:gslin at gslin.com (主要)、darkkiller at gmail.com (也是要)、gslin at gslin.org (備用)。"}

正如您所看到的,正文字段中的 URL 包含未转义的引号。我很好奇是否有一种方法可以确保 MySQL 的输出正确转义这些字符以生成可以由任何 JSON 解析器安全解析的有效 JSON。

我是否应该在 MySQL 查询中使用特定的标志或选项,或者是否有推荐的方法来处理此类数据提取和转换为 JSON?

编辑:根据要求的最小示例:

$ mysql --version
mysql  Ver 8.0.35-27 for Linux on x86_64 (Percona Server (GPL), Release '27', Revision '2f8eeab2'$)
$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.35-27 Percona Server (GPL), Release '27', Revision '2f8eeab2'$

Copyright (c) 2009-2023 Percona LLC and/or its affiliates
Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 8.0.35-27 |
+-----------+
1 row in set (0.00 sec)

创建表并添加数据:

$ mysql -u root -p test
mysql> CREATE TABLE post (id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT, content LONGTEXT NOT NULL);
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO post (content) VALUES ('<a href="https://www.example.com/">www.example.com</a>');
Query OK, 1 row affected (0.00 sec)

然后检查一下:

mysql> SELECT * FROM post;
+----+--------------------------------------------------------+
| id | content                                                |
+----+--------------------------------------------------------+
|  1 | <a href="https://www.example.com/">www.example.com</a> |
+----+--------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT JSON_OBJECT("content", content) FROM post;
+-------------------------------------------------------------------------+
| JSON_OBJECT("content", content)                                         |
+-------------------------------------------------------------------------+
| {"content": "<a href=\"https://www.example.com/\">www.example.com</a>"} |
+-------------------------------------------------------------------------+
1 row in set (0.00 sec)

然后,在shell中(tail -n +2这里是跳过第一行):

$ echo "SELECT JSON_OBJECT('content', content) FROM post;" | mysql -u root -p test | tail -n +2
Enter password: 
{"content": "<a href=\\"https://www.example.com/\\">www.example.com</a>"}

通过验证jq:

$ echo "SELECT JSON_OBJECT('content', content) FROM post;" | mysql -u root -p test | tail -n +2 | jq .
Enter password: 
parse error: Invalid numeric literal at line 1, column 30
mysql
  • 1 1 个回答
  • 26 Views

1 个回答

  • Voted
  1. Best Answer
    Bill Karwin
    2024-02-19T12:33:15+08:002024-02-19T12:33:15+08:00

    所以问题是引号是双重转义的。

    {"content": "<a href=\\"https:...
                         ^^ two backslashes
    

    JSON_OBJECT() 的输出在文字引号字符之前包含一个反斜杠。然后客户端的输出mysql在唯一的反斜杠前面添加另一个反斜杠。

    您可以使用mysql 客户端的--raw(或) 选项来控制它。-r

    https://dev.mysql.com/doc/refman/8.0/en/mysql-command-options.html#option_mysql_raw

    对于非表格输出(例如以批处理模式生成或给出--batchor--silent选项时),特殊字符在输出中进行转义,以便可以轻松识别它们。换行符、制表符、NUL 和反斜杠写为\n, \t, \0, 和\\。该--raw选项禁用该字符转义。

    首先我测试没有--raw它会导致与您得到的相同错误:

    $ echo "SELECT JSON_UNQUOTE(JSON_OBJECT('content', content)) FROM post;" | mysql -b -N test | jq .
    jq: parse error: Invalid numeric literal at line 1, column 30
    

    然后我添加--raw选项(我-r简称):

    $ echo "SELECT JSON_UNQUOTE(JSON_OBJECT('content', content)) FROM post;" | mysql -b -N -r test | jq .
    {
      "content": "<a href=\"https://www.example.com/\">www.example.com</a>"
    }
    
    • 1

相关问题

  • 是否有任何 MySQL 基准测试工具?[关闭]

  • 我在哪里可以找到mysql慢日志?

  • 如何优化大型数据库的 mysqldump?

  • 什么时候是使用 MariaDB 而不是 MySQL 的合适时机,为什么?

  • 组如何跟踪数据库架构更改?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve