Skip to content

How to use mixed case json without enable_case_sensitive_super_attribute

0

My SQL client (PopSQL) does not allow setting parameters per query.

According to RedShift documentation default should be case insensitive but in practice it looks like it does not accept any casing:

with q as (select json_parse('{"loHi":1}') o)
select o."loHi", o.loHi, o.lohi, o."lohi", o."LOHI", o.LOHI from q
lohilohilohilohilohilohi
nullnullnullnullnullnull

Is this a bug?

asked 2 years ago653 views

2 Answers
1
Accepted Answer

In Amazon Redshift, the default value for enable_case_sensitive_identifier and enable_case_sensitive_super_attribute is false.

When JSON attribute names are in uppercase or mixedcase, you must navigate SUPER type structures in a case sensitive way. To do that, you can configure enable_case_sensitive_identifier to TRUE and wrap the uppercase and mixedcase attribute names with double quotation marks. You can also configure enable_case_sensitive_super_attribute to TRUE. This is by design as per the documentation here [1].

Since the SQL client does not allow setting parameters, you can use LOWER_ATTRIBUTE_NAMES function which converts all applicable attribute names in a SUPER value to lowercase as shown below:

with q as (select LOWER_ATTRIBUTE_NAMES(json_parse('{"loHi":1}')) o) select o."loHi", o.loHi, o.lohi, o."lohi", o."LOHI", o.LOHI from q ;

----------------------------------OUTPUT----------------------------

lohi	lohi	lohi	lohi	lohi	lohi
  1      1        1       1       1       1 

Resources:

  1. JSON mixedcase/uppercase attribute names
  2. enable_case_sensitive_identifier documentation
  3. enable_case_sensitive_super_attribute documentation
AWS

answered 2 years ago

EXPERT

reviewed 2 years ago

  • Thanks, the documentation of enable_case_sensitive_super_attribute could be a bit more explicit and say that when it is set to false then mixed case keys are simply ignored.

1

If you cannot use the enable_case_sensitive_super_attribute parameter due to limitations in your SQL client, there are alternative approaches to handle mixed case JSON in Amazon Redshift:

  1. Use the LOWER_ATTRIBUTE_NAMES function: This function converts all attribute names in a SUPER value to lowercase, allowing you to query the data consistently. Here's how you can modify your query:
with q as (select LOWER_ATTRIBUTE_NAMES(json_parse('{"loHi":1}')) as o)
select o.lohi from q

This approach will return the value 1 for the 'lohi' attribute.

  1. Use double quotes and enable_case_sensitive_identifier: If you can set the enable_case_sensitive_identifier parameter to TRUE (which is different from enable_case_sensitive_super_attribute), you can use double quotes to specify the exact case of the attribute name:
SET enable_case_sensitive_identifier to TRUE;

with q as (select json_parse('{"loHi":1}') o)
select o."loHi" from q

This should return the value 1 for the 'loHi' attribute.

  1. Parse the JSON as needed: If the above options are not available, you can parse the JSON string directly in your query:
with q as (select '{"loHi":1}' as json_string)
select json_extract_path_text(json_string, 'loHi') as lohi from q

This approach allows you to specify the exact case of the JSON key without relying on SUPER type or configuration parameters.

By using one of these methods, you should be able to work with mixed case JSON in Redshift without needing to set the enable_case_sensitive_super_attribute parameter.
Sources
LOWER_ATTRIBUTE_NAMES function - Amazon Redshift
SUPER configurations - Amazon Redshift
JSON functions - Amazon Redshift

answered 2 years ago

EXPERT

reviewed 2 years ago

  • Looks like LOWER_ATTRIBUTE_NAMES suggested by AWS AI works although still would like to know if this is a bug

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.