Re: BUG #13937: 'src' -> jsonb_each() -> jsonb_object() -> 'dst' does not recreate 'src' as valid jsonb - Mailing list pgsql-bugs

From Tom Lane
Subject Re: BUG #13937: 'src' -> jsonb_each() -> jsonb_object() -> 'dst' does not recreate 'src' as valid jsonb
Date
Msg-id 8960.1455032797@sss.pgh.pa.us
Whole thread Raw
In response to BUG #13937: 'src' -> jsonb_each() -> jsonb_object() -> 'dst' does not recreate 'src' as valid jsonb  (xtracoder@gmail.com)
Responses Re: BUG #13937: 'src' -> jsonb_each() -> jsonb_object() -> 'dst' does not recreate 'src' as valid jsonb  (Xtra Coder <xtracoder@gmail.com>)
List pgsql-bugs
xtracoder@gmail.com writes:
> Expected result:
> ----------------

> NOTICE:  jsonb_src = {"key1": {"data1": [1, 2, 3]}, "key2": {"data2": [3, 4,
> 5]}}
> NOTICE:  jsonb_dst = {"key1": {"data1": [1, 2, 3]}, "key2": {"data2": [3, 4,
> 5]}}

> 'src' and 'dst' JSON objects should be identical/equal.

Considering that you explicitly casted the "values" to text, it would
absolutely be a bug if you got that result from this code.  I don't think
this is a bug at all; it's a feature request for some new variant of
jsonb_object().  But can't you already get what you want from
jsonb_object_agg()?

DO LANGUAGE plpgsql $$
DECLARE
  jsonb_src jsonb;
  jsonb_dst jsonb;
BEGIN
  jsonb_src = '{
    "key1": {"data1": [1, 2, 3]},
    "key2": {"data2": [3, 4, 5]}
  }';
  raise notice 'jsonb_src = %', jsonb_src;

  with t_data as (select * from jsonb_each(jsonb_src))
  select jsonb_object_agg(key, value) into jsonb_dst
  from t_data;
  raise notice 'jsonb_dst = %', jsonb_dst;
END $$;

NOTICE:  jsonb_src = {"key1": {"data1": [1, 2, 3]}, "key2": {"data2": [3, 4, 5]}}
NOTICE:  jsonb_dst = {"key1": {"data1": [1, 2, 3]}, "key2": {"data2": [3, 4, 5]}}
DO

I think your original coding is kinda broken anyway; there's no hard
guarantee that those two array sub-selects will give results in the
same order.

            regards, tom lane

pgsql-bugs by date:

Previous
From: Joe Conway
Date:
Subject: Re: BUG #13934: wrong result of split_part with char value
Next
From: Tom Lane
Date:
Subject: Re: BUG #13934: wrong result of split_part with char value