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

Reading field of type `JSON` returns invalid value

Open JakobJingleheimer opened this issue 6 months ago • 0 comments

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

JakobJingleheimer avatar Jan 14 '24 18:01 JakobJingleheimer