node-mysql2 icon indicating copy to clipboard operation
node-mysql2 copied to clipboard

re escaping special chars in json values

Open Reaster0 opened this issue 10 months ago • 1 comments

Hi, i've found a weird behaviour, when querying a table and converting it to a JSON_TABLE, if a textual row inside of it contain a \n, it will be re escaped (so it will be given as "\\n"), something that it don't do when querying non JSON_TABLE textual values

an example:

rawQuery( SELECT * FROM JSON_TABLE( ( SELECT content as json FROM logs_related_files WHERE log_id = ? LIMIT 1 ), '$[*]' COLUMNS ( name VARCHAR(255) PATH '$.name', content LONGTEXT PATH '$.content', contentType VARCHAR(255) PATH '$.contentType' ) ) as files WHERE name = ? LIMIT 1 , [id, name]) .then((res) => { if (res[0].length == 0) return response.status(404).send("Not found"); return res[0][0]; }) .catch((e) => { console.log(e.message); return response.status(400).send(e.message); });

in this case, even if in "content" the value is "this is a\ntest" (notice the \n) and i can confirm it by running the exact same sql query using a mysql cli client

the driver will give me a content containing "this is a \\ntest" (notice the \\n)

this issue doesn't appear if i query a non JSON_TABLE value (i get a correct "this is a \ntest" using the driver)

so for now i'm running a replace on the result to reset the correct antislash value but i would prefer not to ducktape it :')

thanks for your hardwork!

Reaster0 avatar Apr 09 '24 09:04 Reaster0

Hi @Reaster0 could you try to reduce your example to something self contained ( including initial schema and data ) that I can try to run and debug locally?

sidorares avatar Apr 23 '24 00:04 sidorares