1

i am trying to query like below,

OPT_VAR JSONB;
select 
json_build_object('CustomerProfile',t) into OPT_VAR 
from
(
select firstName,LastName,Type,Gender
from TABLE_NAME
)t;

My expected output like

{
    "CustomerProfile": {
        "firstName": "Sam",
        "LastName": "Smith",
        "Type": "A1",
        "Gender": "Male"
        }
}

But i am getting something like,

{
    "CustomerProfile": {
        "firstName": "Sam",
         "Gender": "Male",
        "LastName": "Smith",
        "Type": "A1"
        }
}

My only concern is order of the json property.

1
  • 2
    Use json instead of jsonb Commented Jul 7, 2021 at 12:13

2 Answers 2

2

The definition at json.org says:

An object is an unordered set of name/value pairs

So you should not depend on the order of keys in JSON. That's probably why Postgres does not care about the order in the JSON output.

Sign up to request clarification or add additional context in comments.

Comments

0

in the comment as mentioned by a-horse-with-no-name

OPT_VAR JSON; instead of OPT_VAR JSONB;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.