Upper / lower case keys in JSON objects

Started by Mark9 months ago3 messagesgeneral
Jump to latest
#1Mark
nvpostgres@gmail.com

Hi all

Apologies for any unexpected protocol exceptions, I do not post to mailing
lists very often

I have been trying to work with JSON objects and noticed I could not get
the function json_to_record to produce results, unless I had the key values
in lower case, through testing what works.

I completed a search, but could not find any pointer (except for some front
end comments on labels being all upper or all lower case)

Could anybody comment if the postgres standard is to have key labels in
lower case?

Many thanks for any help you can provide

Mark

query:
select * from json_to_record('{"secLvl": 13, "firBal": "somethi", "firLvl":
"C", "thiLvl": "A", "fourLvl": 2}'::json) as x(seclvl int, firbal text,
firlvl text
, thilvl text, fourlvl int);

result:
seclvl | firbal | firlvl | thilvl | fourlvl
--------+--------+--------+--------+---------
| | | |
(1 row)

query:
select * from json_to_record('{"seclvl": 13, "firbal": "somethi", "firlvl":
"C", "thilvl": "A", "fourlvl": 2}'::json) as x(seclvl int, firbal text,
firlvl text
, thilvl text, fourlvl int);

result:
seclvl | firbal | firlvl | thilvl | fourlvl
--------+---------+--------+--------+---------
13 | somethi | C | A | 2
(1 row)

#2Mark
nvpostgres@gmail.com
In reply to: Mark (#1)
Re: Upper / lower case keys in JSON objects

Sorry all,

Figured it out, the following works:

query:
select * from json_to_record('{"secLvl": 13, "firBal": "somethi", "firLvl":
"C", "thiLvl": "A", "fourLvl": 2}'::json) as x("secLvl" int, "firBal" text,
"firLvl
" text, "thiLvl" text, "fourLvl" int);

result:
secLvl | firBal | firLvl | thiLvl | fourLvl
--------+---------+--------+--------+---------
13 | somethi | C | A | 2
(1 row)

On Fri, Jul 25, 2025 at 1:26 PM Mark <nvpostgres@gmail.com> wrote:

Show quoted text

Hi all

Apologies for any unexpected protocol exceptions, I do not post to mailing
lists very often

I have been trying to work with JSON objects and noticed I could not get
the function json_to_record to produce results, unless I had the key values
in lower case, through testing what works.

I completed a search, but could not find any pointer (except for some
front end comments on labels being all upper or all lower case)

Could anybody comment if the postgres standard is to have key labels in
lower case?

Many thanks for any help you can provide

Mark

query:
select * from json_to_record('{"secLvl": 13, "firBal": "somethi",
"firLvl": "C", "thiLvl": "A", "fourLvl": 2}'::json) as x(seclvl int, firbal
text, firlvl text
, thilvl text, fourlvl int);

result:
seclvl | firbal | firlvl | thilvl | fourlvl
--------+--------+--------+--------+---------
| | | |
(1 row)

query:
select * from json_to_record('{"seclvl": 13, "firbal": "somethi",
"firlvl": "C", "thilvl": "A", "fourlvl": 2}'::json) as x(seclvl int, firbal
text, firlvl text
, thilvl text, fourlvl int);

result:
seclvl | firbal | firlvl | thilvl | fourlvl
--------+---------+--------+--------+---------
13 | somethi | C | A | 2
(1 row)

#3Adrian Klaver
adrian.klaver@aklaver.com
In reply to: Mark (#1)
Re: Upper / lower case keys in JSON objects

On 7/25/25 05:26, Mark wrote:

Hi all

Apologies for any unexpected protocol exceptions, I do not post to
mailing lists very often

I have been trying to work with JSON objects and noticed I could not get
the function json_to_record to produce results, unless I had the key
values in lower case, through testing what works.

I completed a search, but could not find any pointer (except for some
front end comments on labels being all upper or all lower case)

Could anybody comment if the postgres standard is to have key labels in
lower case?

Yes and no. It comes down to this:

https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS

"Quoting an identifier also makes it case-sensitive, whereas unquoted
names are always folded to lower case. For example, the identifiers FOO,
foo, and "foo" are considered the same by PostgreSQL, but "Foo" and
"FOO" are different from these three and each other. (The folding of
unquoted names to lower case in PostgreSQL is incompatible with the SQL
standard, which says that unquoted names should be folded to upper case.
Thus, foo should be equivalent to "FOO" not "foo" according to the
standard. If you want to write portable applications you are advised to
always quote a particular name or never quote it.)"

So on the Postgres(SQL) side 'as x(seclvl int, firbal text, firlvl text
, thilvl text, fourlvl int)' if you want to match the mixed case keys
coming from the JSON object you need to double quote the field names.

The yes is that Postgres by default folds identifiers to lower case, but
no that does not mean you have to use lower case by default for the JSON
keys.

Many thanks for any help you can provide

Mark

query:
select * from json_to_record('{"secLvl": 13, "firBal": "somethi",
"firLvl": "C", "thiLvl": "A", "fourLvl": 2}'::json) as x(seclvl int,
firbal text, firlvl text
, thilvl text, fourlvl int);

result:
seclvl | firbal | firlvl | thilvl | fourlvl
--------+--------+--------+--------+---------
       |        |        |        |
(1 row)

query:
select * from json_to_record('{"seclvl": 13, "firbal": "somethi",
"firlvl": "C", "thilvl": "A", "fourlvl": 2}'::json) as x(seclvl int,
firbal text, firlvl text
, thilvl text, fourlvl int);

result:
seclvl | firbal  | firlvl | thilvl | fourlvl
--------+---------+--------+--------+---------
    13 | somethi | C      | A      |       2
(1 row)

--
Adrian Klaver
adrian.klaver@aklaver.com