TryGhost / node-sqlite3

SQLite3 bindings for Node.js
BSD 3-Clause "New" or "Revised" License
6.23k stars 817 forks source link

Reading field of type `JSON` returns invalid value #1749

Open JakobJingleheimer opened 10 months ago

JakobJingleheimer commented 10 months ago

Issue Summary

I have a field (tags) that is of type JSON. I insert a record with the json value stringified. That appears to succeed. However, when I read the record back out, the value is missing inner quotes around values that need them.

The actual result is unusable.

Steps to Reproduce

await db.run(`
CREATE TABLE IF NOT EXISTS foo (
  _id CHAR(6) NOT NULL PRIMARY KEY,
  tags JSON DEFAULT []
);
`);
const _id = uid(6);
const tags = ["groceries"];
await db.run(`INSERT INTO foo (_id,tags) VALUES (${uid},${JSON.stringify(tags)});`);
const record = await db.run(`SELECT * FROM foo WHERE _id = ${uid};`);

console.log(record);

Actual result:

{
  _id: 'a96ce3',
  tags: '[groceries]',
}

Expected result:

{
  _id: 'a96ce3',
  tags: '["groceries"]',
}

OR

{
  _id: 'a96ce3',
  tags: ["groceries"],
}

Version

5.1.7

Node.js Version

21.5.0

How did you install the library?

npm i -S sqlite3