M
M
Mark N2020-11-25 15:49:07
PostgreSQL
Mark N, 2020-11-25 15:49:07

How to parse a nested Postgres JSONB[] array?

I have a table

CREATE TABLE posts (
    id uuid PRIMARY KEY,
    tags jsonb[]
);


It has content that looks like this
5fbe51275a450506928068.png

I need to parse the tags column
{"{\"color\": 35, \"number\": 21}","{\"color\": 54, \"number\": 2}"}


Tell me how I can parse such content by SQL query columns, so that it would look something like this.

post_id    |  color           | number 
**         |  54              | 21      
**         |  35              | 2


How should I write a SQL query?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dr. Bacon, 2020-11-25
@bacon

Did you try to read the docs?

M
Melkij, 2020-11-25
@melkij

select id, t->'color', t->'number' from posts cross join unnest(tags) as t;

It's a rather strange idea to do jsonb[]. Usually they make an array inside json, not an array from json.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question