1

How can I extract values from the json arrays in ranges column as multiple rows Postgresq?

CREATE TABLE test_table (
  id INTEGER, 
  ranges jsonb
);

INSERT INTO test_table(id, ranges) VALUES
(1,'[{"End": 100, "Start": 1}, {"End": 1000, "Start": 101}]'),
(2,'[{"End": 2000, "Start": 1001}, {"End": 2002, "Start": 2001}]')
;

Expected result:

Start End
1 100
101 1000
1001 2000
2001 2002

1 Answer 1

3

You can use jsonb_to_recordset function for this :

SELECT ranges."Start",
       ranges."End"
FROM   test_table,
       jsonb_to_recordset(test_table.ranges) AS ranges("End" int, "Start" int)

http://www.sqlfiddle.com/#!17/22d62/10

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.