I'm trying to insert a JSON object into a MySQL JSON column and I'm getting: Invalid JSON text: "Invalid escape character in string."
The offending character is the \n in this part of the object:
"summary":"Summary of Qualifications:\nAccomplished and results-driven General Business Director with a consistent track record."
I've tried JSON.stringify, JSON_OBJECT, JSON_QUOTE, and my most recent attempts included:
summary.replace("\n", "\\n") and variations on replacing the existing escape characters with proper? escape characters.
I'm using Node, Express and JawsDB if that may have any bearing.
EDIT: Here's the actual insert code:
const fields = '(myField)';
const values = `('${JSON.stringify(field.contents || {})}')`;
db.query('INSERT INTO mydb ' + fields + ' VALUES ' + values,
err => {
if (err) {
console.log(values);
throw(err) ;
}
}
);
\nis the problem? How are you actually doing the insert? Placeholders? (e.g.VALUES(?))??insideVALUES(). Use this:db.query('INSERT INTO mydb ' + fields + ' VALUES (?)', [ JSON.stringify(field.contents || {}) ], err ...You should never concatenate string literals into queries. It is unsafe and (as you now see) error-prone.