jsonb_object() seems to be buggy. jsonb_build_object() is good. - Mailing list pgsql-hackers

From Bryn Llewellyn
Subject jsonb_object() seems to be buggy. jsonb_build_object() is good.
Date
Msg-id E1529841-8E71-4CA1-8B75-95527086D820@yugabyte.com
Whole thread Raw
Responses Re: jsonb_object() seems to be buggy. jsonb_build_object() is good.
List pgsql-hackers
Execute this:

select jsonb_pretty(jsonb_build_object(
  'a'::varchar, 1.7::numeric,
  'b'::varchar, 'dog'::varchar,
  'c'::varchar, true::boolean
  ))

It produces the result that I expect:

 {              +
     "a": 1.7,  +
     "b": "dog",+
     "c": true  +
 }

Notice that the numeric, text, and boolean primitive values are properly rendered with the text value double-quoted and
thenumeric and boolean values unquoted. 

Now execute this supposed functional equivalent:

select jsonb_pretty(jsonb_object(
  '{a, 17, b, "dog", c, true}'::varchar[]
  ))

It is meant to be a nice alternative when you want to build an object (rather than an array) because the syntax is less
verbose.

However, it gets the wrong answer, thus:

 {              +
     "a": "17", +
     "b": "dog",+
     "c": "true"+
 }

Now, the numeric value and the boolean value are double-quoted—in other words, they have been implicitly converted to
JSONprimitive text values. 

Do you agree that this is a bug?

Notice that I see this behavior in vanilla PostgreSQL 11.2 and in YugabyteDB Version 2.0.11.0. See this blogpost:

“Distributed PostgreSQL on a Google Spanner Architecture—Query Layer”
https://blog.yugabyte.com/distributed-postgresql-on-a-google-spanner-architecture-query-layer/

YugabyteDB uses the PostgreSQL source code for its SQL upper half.

Regards, Bryn Llewellyn, Yugabyte




pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: Use LN_S instead of "ln -s" in Makefile
Next
From: Vik Fearing
Date:
Subject: Re: jsonb_object() seems to be buggy. jsonb_build_object() is good.