简体   繁体   中英

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: \\\\"

I'm trying to export this table to mongodb, but when I import the JSON file mongodb throws to me this error:

Failed: error processing document #18: invalid character 't' after object key:value pair

this is my query: 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:

{
    "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 .

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:

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';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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