May'Habit
May'Habit

Reputation: 1372

How to union json string with row_to_json

I have a query like this:

        select row_to_json(a) from (
            select id, title, body, page_total from TMP 
            offset page_size*(page-1)
            limit page_size
        ) a;

And other query like this:

select '{"a":1,"b":2}'

How do I union these selects? When I try this:

select '{"a":"1"}'::json
union
select row_to_json(a) from (
    select id, title, body from news
    offset 0
    limit 5
) a;

It gets error: ERROR: could not identify an equality operator for type json LINE 1: select '{"a":"1"}'::json ^ SQL state: 42883 Character: 8

Upvotes: 0

Views: 314

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 246318

Use jsonb instead:

select '{"a":"1"}'::jsonb
union
select to_jsonb(a) from (
    select id, title, body from news
    offset 0
    limit 5
) a;

Upvotes: 1

Related Questions