question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Insert array into a JSON column

See original GitHub issue

This query fails

var q = adapter.query('INSERT INTO tbl_data (id, data) VALUES ($1, $2)', [ 1, ['a', 'b'] ] );

with

 error: undefined
      at Connection.parseE (/home/user/dev/node_modules/pg.js/lib/connection.js:534:11)
      at Connection.parseMessage (/home/user/dev/node_modules/pg.js/lib/connection.js:361:17)
      at Socket.<anonymous> (/home/user/dev/node_modules/pg.js/lib/connection.js:105:22)
      at Socket.emit (events.js:107:17)
      at readableAddChunk (_stream_readable.js:163:16)
      at Socket.Readable.push (_stream_readable.js:126:10)
      at TCP.onread (net.js:538:20)

The table is defined as

CREATE TABLE tbl_data
(
  id bigserial NOT NULL,
  data json,
  CONSTRAINT "PK_data" PRIMARY KEY (id)
)

(Note: the table is for tests only, and it illustrate the problem I am having in my project.)

Also, see #864

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
yanickrochoncommented, Sep 25, 2015

The only solution I can think of is to execute the query like this :

var q = adapter.query('INSERT INTO tbl_data (id, data) VALUES ($1, $2)', [ 1, JSON.stringify(['a', 'b']) ] );

Which seems to work.

0reactions
yanickrochoncommented, Feb 11, 2016

Alright, closing this, now. It was a - character on a different line that caused NaN to be sent (i.e. -{}) This is not pg’s fault.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding a new array element to a JSON object - Stack Overflow
In my case, my JSON object didn't have any existing Array in it, so I had to create array element first and then...
Read more >
INSERT array elements from JSON object as table rows
Something like this: INSERT INTO tbl (id, type) -- spell out actual column names! SELECT 1708, json_array_elements_text('{"id":1708, ...
Read more >
16.20 - Examples: Inserting Values into a JSON Column
The example creates a table with a JSON column, allocates and initializes a JSON instance using the JSON constructor, then inserts the JSON...
Read more >
MySQL 8.0 Reference Manual :: 11.5 The JSON Data Type
A JSON array contains a list of values separated by commas and enclosed ... Attempting to insert a value into a JSON column...
Read more >
Inserting into a JSON column in mysql - Laracasts
I have a table defined that has 2 JSON columns and the rest strings. How do I insert key values from an associative...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found