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.

Error: invalid input syntax for type json

See original GitHub issue

I have the following situation. I am using the pg driver version 4.5.3 (yes I know its old, but can’t yet refactor or switch to new version), along with Node 4.x, Express 4, and a postgres 9.5 database. I have a table that looks like this: code to recreate(https://gist.github.com/PythonDevOp/aced5918de770365258690cecbceee92)

 id          |   x        |   y       |   row    |   col
--------+--------+------+--------+------------
    1        |  5        |     1     |    2      |  1
    2        |  6        |     2     |    3      |  2
    3        |  7        |     3     |    4      |  7
    4        |  8        |     4     |    5      |  9

I am calling a function, taking an array of data, and passing that array as a parameter using commands in the following gist: https://gist.github.com/PythonDevOp/f7a7f95fd7fa11a06420c54c5be4501a

However, I am getting an error when trying to insert the data via the node postgres driver. When I call the function in pgadmin or via the console, it works as expected. When calling this via the application, something breaks and I can’t tell where. I am guessing this is a bug with the postgres driver in how it parses JSONB.

Any ideas?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:7

github_iconTop GitHub Comments

4reactions
joskuijperscommented, Oct 19, 2016

I found the problem.

When parsing the value to something postgres understands, the input is checked on its type. Whet it is an object, it is stringified using JSON.stringify (as we want). However, when the input is an array, it is transformed into Postgres arrays ({'a','b'}). Ergo, the input becomes {'key',..... Parsed this as JSON gives the error you get: expected : when getting a comma.

To solve this, there would need to be a check on the true input type (when using ::type). If this typecast is not used there is no way to know whether to parse the data as array or as object. Sadly, the content of the query string is not used anywhere in node-postgres currently.

I am open to solutions.

0reactions
charmandercommented, Feb 8, 2017

Reopened #442.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to prevent 'invalid input syntax for type json' in Postgres ...
If you want to skip the rows with invalid JSON, you must first test if the text is valid JSON. You can do...
Read more >
Editing JSON field with string throws "invalid input syntax for ...
Create a collection field with the following settings: Type: JSON; Interface: Code; Display: Formatted JSON value. Errors Shown. Unexpected ...
Read more >
Using variable in jsonb - Get Help - Metabase Discussion
I've got this error: ERROR: invalid input syntax for type json Detail: Token "'" is invalid. Where: JSON data, line 1: '.
Read more >
Postges: 'invalid input syntax for type json' - Wappler Community
I have validated that the JSON i'm trying to insert into a JSONB column is indeed valid, and without error. Any advice what...
Read more >
#29147 (Postgres JSONField missing to_python) – Django
will_test=# select 'hello'::json ; ERROR: invalid input syntax for type json LINE 1: select 'hello'::json ; ^ DETAIL: Token "hello" is invalid.
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