107

On PostgreSQL 9.3.4, I have a JSON type column called "person" and the data stored in it is in the format {dogs: [{breed: <>, name: <>}, {breed: <>, name: <>}]}. I want to retrieve the breed of dog at index 0. Here are the two queries I ran:

Doesn't work

db=> select person->'dogs'->>0->'breed' from people where id = 77;
ERROR:  operator does not exist: text -> unknown
LINE 1: select person->'dogs'->>0->'bree...
                                 ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

Works

select (person->'dogs'->>0)::json->'breed' from es_config_app_solutiondraft where id = 77;
 ?column?
-----------
 "westie"
(1 row)

Why is the type casting necessary? Isn't it inefficient? Am I doing something wrong or is this necessary for postgres JSON support?

ravishi
  • 3,349
  • 5
  • 31
  • 40

1 Answers1

181

This is because operator ->> gets JSON array element as text. You need a cast to convert its result back to JSON.

You can eliminate this redundant cast by using operator ->:

select person->'dogs'->0->'breed' from people where id = 77;
max taldykin
  • 12,459
  • 5
  • 45
  • 64
  • 14
    Don't forget to review the full list of JSON operators supported by PG https://www.postgresql.org/docs/current/static/functions-json.html – Edgar Ortega Apr 17 '18 at 20:32
  • 4
    what if you needed a list of all the breeds? is there support for something like `select person->'dogs'->*->'breed'` – mga Feb 11 '20 at 05:50
  • 4
    @mga, see [json_array_elements](https://www.postgresql.org/docs/current/functions-json.html#FUNCTIONS-JSON-PROCESSING-TABLE) function. `select dog->'breed' from people p, json_array_elements(p->'dogs') dog` – max taldykin Feb 11 '20 at 07:11