AbrA
AbrA

Reputation: 480

how to transfer row to jsonb and set name of keys?

I have a table person with fields: id, first_name, last_name.
Query:

select COALESCE( array_to_json(array_agg(row_to_json(data))), '{}'::json)
from (select id, first_name as firstName from person) data)

return this result:

  {
      "id": 1,
      "firstname": Alex
  }

how to get a result:

  {
      "id": 1,
      "firstName": Alex
  }

Upvotes: 0

Views: 74

Answers (1)

user330315
user330315

Reputation:

Unquoted identifiers are folded to lower case in Postgres, so you need to use quoted identifier:

select COALESCE(jsonb_agg(to_jsonb(data)), '{}'::jsonb)
from (
   select id, first_name as "firstName" 
   from person
) data

Upvotes: 1

Related Questions