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.

Returning Invalid syntax for type json on arrays

See original GitHub issue

When your column is type json and you try and insert a JSON array, you get the following error:

{ [error: invalid input syntax for type json]
  length: 154,
  name: 'error',
  severity: 'ERROR',
  code: '22P02',
  detail: 'Expected string or "}", but found "1".',
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: 'JSON data, line 1: {1...',
  file: 'json.c',
  line: '665',
  routine: 'report_parse_error' }

For example, this works in PSQL:

insert into my_table ( data ) values ( '[1,2,3]' );

But this returns an error in node-pg:

client.query('insert into my_table ( data ) values ( $1 )', [ [1,2,3] ], function( error ){
  /* ERROR */
});

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:5
  • Comments:28 (10 by maintainers)

github_iconTop GitHub Comments

22reactions
booocommented, Sep 16, 2013

We convert javascript arrays into postgres arrays. If you stringify your input array it should work:

client.query('INSERT INTO my_table (data) VALUES ($1)', [ JSON.stringify([1,2,3])], handler);

We don’t now the column type in advance so we can’t do a auto conversions of the array.

17reactions
tamlyncommented, Jun 29, 2017

Would it make sense for node-postgres to intercept this error and augment it with something like Are you inserting an array into a JSON column? See <link to docs>? Helpful error messages make for a nice developer experience.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Returning Invalid syntax for type json on arrays #442
Just because pg doesn't know the column type in advance does not mean you should convert to the syntax of column type "array"....
Read more >
Update of value in array of jsonb returns error"invalid input ...
Until now I always get the following error: invalid input syntax for type json DETAIL: Token "c66a4353" is invalid. CONTEXT: JSON data, line...
Read more >
Returning Invalid syntax for type json on arrays - Bountysource
When your column is type json and you try and insert a JSON array, you get the following error: { [error: invalid input...
Read more >
JSON.stringify() - JavaScript - MDN Web Docs
If replacer is an array, all elements in this array that are not strings or numbers (either primitives or wrapper objects), including Symbol ......
Read more >
JSON Syntax - W3Schools
JSON Values. In JSON, values must be one of the following data types: a string; a number; an object; an array; a boolean;...
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