Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SequelizeJS - insert JSONB to Postgres with raw query - how to escape

I want to insert a row with JSONB content as a raw query. The problem is that I get error wherever I insert any "nontrivial" JSON object.

These are examples of my raw SQL queries:

Error for all of these: SequelizeBaseError: invalid input syntax for type json

'INSERT INTO "sometable" ("id", "createdAt", "updatedAt", "custom_data") VALUES (DEFAULT, NOW(), NOW(), \'"{something:\"value\"}"\')';
'INSERT INTO "sometable" ("id", "createdAt", "updatedAt", "custom_data") VALUES (DEFAULT, NOW(), NOW(), \'"{\"aaa\":\"ss15\"}"\')';

This last one works, but only with integer values:

'INSERT INTO "sometable" ("id", "createdAt", "updatedAt", "custom_data") VALUES (DEFAULT, NOW(), NOW(), \'"{a:10}"\')';

I've tried so many possible combinations of escaping the JSON-like string, even inserting real JS object, and nothing worked.

like image 700
user3696212 Avatar asked Mar 09 '23 04:03

user3696212


1 Answers

Try to JSON.stringify() first:

var customData = JSON.stringify({"name":"George Washington","presidentNo":1});
like image 181
Avraham Avatar answered Apr 27 '23 17:04

Avraham