OneBite.Dev - Coding blog in a bite size

Convert data from jsonb postgres to array Javascript

Here is how to Convert data from jsonb postgres to array Javascript

If you’re using Postgres as database and storing one of the data’s column in jsonb type. Or to be exact, storing the data as array of objects.

Like this for example:

[
    {"event_id": "489nsmnur5rud4l64t1sj6hd7g", "calendar_id": 9},
    {"event_id": "489nsmnur5rud4l64t1sj6hd7g", "calendar_id": 10}
]

Here is how you can convert that data after query (fetch the from database) either throught ORM library like Prisma or natively.

const data = Object.values(ColumnDataFromDB) // convert jsonbPostgres to array
for(let i = 0; i < data.length; i++) {
    ...
}
PostgreSQL javascript