4

So, I can email address, password and date using this code in node

client.query("INSERT INTO manlalaro (emailaddr,pwd,premiumexpiry) values ('[email protected]','123absurdcodes', DATE '2009-09-19') ",(err,res)=>{
   console.log(err, res)
   client.end()
})

But how do I enter JSON data type successfully without getting errors? I have playersaved which is a data type JSON. enter image description here

7
  • Postgres have a specific type for JSON. postgresql.org/docs/9.4/datatype-json.html Commented Nov 12, 2019 at 16:48
  • 1
    How are you doing the insert of the JSON data? Which errors are occurring? Commented Nov 12, 2019 at 16:52
  • @JDuwe I tried using this code. ` client.query("INSERT INTO manlalaro (emailaddr,pwd,premiumexpiry,playersaved) values ('[email protected]','123absurdcodes', DATE '2009-09-19', {"items":1,"level":7,"description":"Small ax used for harvesting wood."}) ",(err,res)=>{ console.log(err, res) client.end() })` Commented Nov 12, 2019 at 21:30
  • Which errors are occuring? Commented Nov 13, 2019 at 11:55
  • 1
    @Aedric That code is missing escaping of the " in the JS string literal. Use \", or concatenate with JSON.stringify. And you will need to put the json text in a postgres literal value notation, i.e. '{…}' - notice the apostrophes (just like around an ordinary postgres string). But better follow JDuwe's advice and let node-pg do the escaping :-) Commented Nov 13, 2019 at 21:00

2 Answers 2

2

The best way to pass the data to be inserted in a separated parameter where the library or the driver do the right treatment to each data type.

In most cases it will be something like this:

client.query("INSERT INTO x (a, b, c) VALUES (?, ?, ?)", [1, "text", { "json": "data" }]);

Or this:

client.query("INSERT INTO x (a, b, c) VALUES ($1, $2, $3)", [1, "text", { "json": "data" }]);

The way to know the right thing to do is read the documentation of the library.

If you are using pg (node-postgres) https://node-postgres.com/

Note: As @Aedric pointed out, in some cases your object must be previously "stringified" (JSON.stringify()). But node-postgres claims it do this automatically. (https://node-postgres.com/features/types#uuid%20+%20json%20/%20jsonb).

Sign up to request clarification or add additional context in comments.

2 Comments

thank you so much! I am a bit new to that ($1, $2, $3) thing and I found out it only for node postgresql. Also, the json entry { "json": "data" } should be '{ "json": "data" }'. I think the error is cause by my nest array client.query("INSERT INTO manlalaro (userid,emailaddr,pwd,savegame) values ($1,$2,$3,$4)" , [13,'[email protected]','290cgyr789','{"ign":"leroy jenkins","level":7,"poisoned status":false}' ] ,(err,res)=>{ console.log(err, res) client.end() }) Thanks again~!
@Aedric I'm glad it worked. About the json i think it may vary, as the docs says it is automatically converted with JSON.stringify() (node-postgres.com/features/types#uuid%20+%20json%20/%20jsonb), but i'll add a note in the answer about this.
2

You can insert JSON data in postgresql by converting it into string using JSON.stringify(Object)

`insert into tableName (id,json_data) values(1,'{"test":1}')`

5 Comments

var equipment = JSON.stringify( {"items":1,"level":7,"description":"Small ax used for harvesting wood."}) client.query("INSERT INTO manlalaro (emailaddr,pwd,premiumexpiry,playerSaved) values ('[email protected]','123absurdcodes', DATE '2009-09-19', equipment) ", (err,res)=>{ console.log(err, res) client.end() }) It didnt work.
There is no issue in the JSON data. Can you please show me the error?
this works for me client.query("INSERT INTO manlalaro (userid,emailaddr,pwd,savegame) values ($1,$2,$3,$4)" , [13,'[email protected]','290cgyr789','{"ign":"leroy jenkins","level":7,"poisoned status":false}' ] no error on this one but I haven't tried using nest array due to SyntaxError: Unexpected token ':'
This works because this is json stringified. can you show me the query for nested array?
it turns out I missed a bracket in my first 2 attempts. my third attempt I used this {"ign":"leroy jenkins","level":50,"armor aura":[{"code":31,"dataId":1,"value":0},{"code":22,"dataId":0,"value":-0.1},{"code":33,"dataId":0,"value":-5}], "poisoned status":false} and its fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.