brianc/node-pg-types

Problem parsing multidimensional json arrays

Closed this issue · 2 comments

I have a column defined as a jsonb[] type. I'm actually going to store 2D arrays of JSON objects like the following.

[
    [
        {"count": 2, type: "king"},
        {"count": 3, type: "queen"}
    ], [
        {"count": 2, type: "king"},
        {"count": 3, type: "queen"}
    ]
]

The data representation of this on the database is like this...

{{"{\"type\": \"king\", \"count\": 2}","{\"type\": \"queen\", \"count\": 3}"},{"{\"type\": \"king\", \"count\": 2}","{\"type\": \"queen\", \"count\": 3}"}}

When trying to retrieve this kind of data structure from the database however I end up getting the following error.

SyntaxError: Unexpected token , in JSON at position 28
    at JSON.parse (<anonymous>)
    at c:\vagrant\aws\property_manager_api\property_api\node_modules\pg-types\lib\textParsers.js:113:45
    at Array.map (<anonymous>)
    at Array.parseJsonArray (c:\vagrant\aws\property_manager_api\property_api\node_modules\pg-types\lib\textParsers.js:113:14)
    at Result.parseRow (c:\vagrant\aws\property_manager_api\property_api\node_modules\pg\lib\result.js:73:36)
    at Query.handleDataRow (c:\vagrant\aws\property_manager_api\property_api\node_modules\pg\lib\query.js:91:24)
    at Connection.<anonymous> (c:\vagrant\aws\property_manager_api\property_api\node_modules\pg\lib\client.js:217:22)
    at emitOne (events.js:116:13)
    at Connection.emit (events.js:211:7)
    at Socket.<anonymous> (c:\vagrant\aws\property_manager_api\property_api\node_modules\pg\lib\connection.js:123:12)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at Socket.Readable.push (_stream_readable.js:208:10)

I believe this might be because it's ambiguous as to whether or not the curly braces are for an object or an array. Note that I haven't declared the column as a jsonb[][] because internally for postgres it should be treated the same.

Is there any fix for this?

Note that I haven't declared the column as a jsonb[][] because internally for postgres it should be treated the same.

Really…? I think you do need to declare it as a jsonb[][] if that’s what you want.

I think the documentation says otherwise. There are a lot of tools that actually depend on this behaviour as well such as PGAdmin. It won't even let you declare a column as jsonb[][] unless you over ride their gui and write a SQL query to declare the column type as such.

So is the current implementation the expected behaviour?

https://www.postgresql.org/docs/10/static/arrays.html#ARRAYS-DECLARATION