简体   繁体   English

从mysql表替换字符以导出到JSON文件

[英]Replacing a character from mysql table to export to JSON file

I have a very long sized table (over 4M of records,i work with MySql) and it has a lot of records with this string: \\\\" 我有一个非常长的表(超过4M的记录,我使用MySql),它有很多记录与这个字符串: \\\\“

I'm trying to export this table to mongodb, but when I import the JSON file mongodb throws to me this error: 我正在尝试将此表导出到mongodb,但是当我导入JSON文件时,mongodb会向我抛出此错误:

Failed: error processing document #18: invalid character 't' after object key:value pair 失败:错误处理文档#18:对象键后的字符't'无效:值对

this is my query: MySQL 这是我的疑问:MySQL

SELECT json_object(
     "id", id,
     "execution_id", execution_id,
     "type", type,
     "info", info,
     "position", position,
     "created_at", json_object("$date", DATE_FORMAT(created_at,'%Y-%m-%dT%TZ')),
     "updated_at", json_object("$date", DATE_FORMAT(updated_at,'%Y-%m-%dT%TZ'))
      )as 'json'
      FROM myTable
      INTO OUTFILE 'myPath';

I know the problem is the string, my question is: how can I change this certain string to \\" ? Manually change it´s not an option, and my knowledge about query is limited. Please help. Thank you for reading me . 我知道问题是字符串,我的问题是:如何将这个字符串更改为 ?”手动更改它不是一个选项,我对查询的了解有限。请帮助。感谢您阅读我。

The column that has this character is "info", here is an example: 具有此字符的列是“info”,这是一个示例:

{
    "id": 30, 
    "execution_id": 2, 
    "type": "PHASE", 
    "info": "{  \\r\\n \\"title\\": \\"Phase\\",
                \\r\\n \\"order\\": \\"1\\",
                \\r\\n \\"description\\": \\"Example Phase 1\\",
                \\r\\n \\"step\\": \\"end\\",
                \\r\\n \\"status\\": \\"True\\"\\r\\n}",
    "position": 24, 
    "created_at": {"$date": "2018-01-11T15:01:46Z"}, 
    "updated_at": {"$date": "2018-01-11T15:01:46Z"}
}

You should be able to do this using the MySQL REPLACE() function . 您应该能够使用MySQL REPLACE()函数执行此操作。

The backslash is a bit of a special case in the MySQL REPLACE() function, so you will need to use \\\\ to represent each literal \\ , thus to replace \\\\ with \\ you need to run something like this: 反斜杠在MySQL REPLACE()函数中有点特殊情况,因此您需要使用\\\\来表示每个文字\\ ,因此用\\替换\\\\需要运行如下所示:

REPLACE(info,'\\\\\\\\','\\\\')

Your full query would look something like this: 您的完整查询将如下所示:

SELECT json_object(
     "id", id,
     "execution_id", execution_id,
     "type", type,
     "info", REPLACE(info,'\\\\','\\'),
     "position", position,
     "created_at", json_object("$date", DATE_FORMAT(created_at,'%Y-%m-%dT%TZ')),
     "updated_at", json_object("$date", DATE_FORMAT(updated_at,'%Y-%m-%dT%TZ'))
      )as 'json'
      FROM myTable
      INTO OUTFILE 'myPath';

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

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