152

I have a json array stored in my postgres database. The json looks like this:

[
    {
        "operation": "U",
        "taxCode": "1000",
        "description": "iva description",
        "tax": "12"
    },
    {
        "operation": "U",
        "taxCode": "1001",
        "description": "iva description",
        "tax": "12"
    },
    {
        "operation": "U",
        "taxCode": "1002",
        "description": "iva description",
        "tax": "12"
    }
]

Now I need to SELECT the array so that any element is in a different row of the query result. So the SELECT statement I perform must return the data in this way:

 data
--------------------------------------------------------------------------------------
{ "operation": "U", "taxCode": "1000", "description": "iva description", "tax":"12"}
{ "operation": "U", "taxCode": "1001", "description": "iva description", "tax":"12"}
{ "operation": "U", "taxCode": "1002", "description": "iva description", "tax":"12"}

I tried using the unnest() function

SELECT unnest(json_data::json)
FROM my_table

but it doesn't accept the jsonb type.

2
  • 21
    unnest() is for PostgreSQL's array types. Use json_array_elements(json) (9.3+), jsonb_array_elements(jsonb) (9.4+) or json[b]_array_elements_text(json[b]) (9.4+)
    – pozs
    Commented Mar 23, 2016 at 10:22
  • Thank you. If you aswer the question I can accept it.
    – k4ppa
    Commented Mar 23, 2016 at 17:03

3 Answers 3

190

I post the answer originally written by pozs in the comment section.

unnest() is for PostgreSQL's array types.

Instead one of the following function can be used:

  • json_array_elements(json) (9.3+)
  • jsonb_array_elements(jsonb) (9.4+)
  • json[b]_array_elements_text(json[b]) (9.4+)

Example:

select * from json_array_elements('[1,true, [2,false]]')

output value

 -------------
 | 1         |
 -------------
 | true      |
 -------------
 | [2,false] |
 -------------

Here where the documentation for v9.4 can be found.

1
  • 4
    its also worthwhile mentioning that the functions with the "_text" remove the annoying quotes from JSON strings
    – Roman M
    Commented Apr 13, 2020 at 10:05
82

More difficult example:

Suppose you have a table with rows containing jsonb array each and you wish to splat (or unnest) all that arrays and do some aggregate calculations on records contained in them.

Table (let it be categories):

 id | specifics (jsonb)
-----------------------------------------------------------------------------------
  1 | [{"name": "Brand", "required": true}, {"name": "Color", "required": false}]
  2 | [{"name": "Brand", "required": false}, {"name": "Color", "required": false}]

So, if you want to count, how many required specifics you have, you will need to use such query:

SELECT specs.name, COUNT(*) AS total
FROM 
  categories, 
  jsonb_to_recordset(categories.specifics) AS specs(name jsonb, required boolean)
WHERE 
  specs.required = TRUE
  -- AND any other restrictions you need
GROUP BY specs.name
ORDER BY total DESC;

Here FROM x, function(x.column) is a shortened form of a lateral join which effectively joins every row from categories with virtual table created by jsonb_to_recordset function from jsonb array in that same row.

And result will be:

 name  | total
---------------
 Brand |     1

Link to DB Fiddle: https://www.db-fiddle.com/f/c4xZcEgg9dsPVDtE7Keovv/0

1
  • 1
    Great example of using lateral joins
    – Learner
    Commented Aug 13, 2019 at 6:43
60

I would suggest using the json_to_recordset command in your case. Your SQL should then be:

select *
from json_to_recordset('[{"operation":"U","taxCode":1000},{"operation":"U","taxCode":10001}]')
as x("operation" text, "taxCode" int);

The output is:

------------------------
|   |operation|taxCode |
------------------------
| 1 |   "U"   |   1000 |
------------------------
| 2 |   "U"   |  10001 |
------------------------

The columns (or JSON keys) of the example can be freely further expanded.

4
  • How it possible on object? means i want convert select * from json_to_recordset('{"operation":"U","taxCode":1000}') as x("operation" text, "taxCode" int); this but json_to_recordset not work Commented Sep 17, 2019 at 5:41
  • Excellent! Thanks. Commented Dec 14, 2020 at 18:31
  • @SherinGreen Doesnt make sense to convert a object to a record SET, what you expect exactly? Perhaps there is another solution
    – lucaswxp
    Commented Apr 28, 2021 at 18:04
  • @SherinGreen you can use json_each_text(json) or jsonb_each_text(jsonb) - to map object as key value records. or you can wrap an object in array first to match with solution in the answer.
    – Vlad D.
    Commented Apr 30, 2021 at 0:39

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.