Error: invalid input syntax for type json
See original GitHub issueI 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:
- Created 7 years ago
- Reactions:1
- Comments:7
Top GitHub Comments
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.
Reopened #442.