Loading JSON data to AWS Redshift results in NULL values
Asked Answered
C

6

15

I am trying to perform a load/copy operation to import data from JSON files in an S3 bucket directly to Redshift. The COPY operation succeeds, and after the COPY, the table has the correct number of rows/records, but every record is NULL !

It takes the expected amount of time for the load, the COPY command returns OK, the Redshift console reports successful and no errors... but if I perform a simple query from the table, it returns only NULL values.

The JSON is very simple + flat, and formatted correctly (according to examples I found here: http://docs.aws.amazon.com/redshift/latest/dg/r_COPY_command_examples.html)

Basically, it is one row per line, formatted like:

{ "col1": "val1", "col2": "val2", ... }
{ "col1": "val1", "col2": "val2", ... }
{ "col1": "val1", "col2": "val2", ... }

I have tried things like rewriting the schema based on values and data types found in the JSON objects and also copying from uncompressed files. I thought perhaps the JSON was not being parsed correctly upon load, but it should presumably raise an error if the objects cannot be parsed.

My COPY command looks like this:

copy events from 's3://mybucket/json/prefix' 
with credentials 'aws_access_key_id=xxx;aws_secret_access_key=xxx'
json 'auto' gzip;

Any guidance would be appreciated! Thanks.

Clem answered 30/6, 2015 at 1:24 Comment(0)
C
32

So I have discovered the cause - This would not have been evident from the description I provided in my original post.

When you create a table in Redshift, the column names are converted to lowercase. When you perform a COPY operation, the column names are case sensitive.

The input data that I have been trying to load is using camelCase for column names, and so when I perform the COPY, the columns do not match up with the defined schema (which now uses all lowercase column names)

The operation does not raise an error, though. It just leaves NULLs in all the columns that did not match (in this case, all of them)

Hope this helps somebody to avoid the same confusion!

Clem answered 1/7, 2015 at 3:57 Comment(4)
This is the same issue I found after some digging. But I was wondering if there was documentation / solution where you could tell it to ignore case, or convert it. Changing the json key format will be quite a pain with the volume I'm dealing with. edit: never mind, you'll have to use the jsonPaths solutionMurray
I stumbled across this error because a NOT NULL column was saying my JSON had no value for it, which was wrong. A quick Google search landed here. I'd say accept this answer as it was a huge help for a crucial component of a pipeline I'm working on. I will see about forwarding a request to the Amazon team via ticket to support case insensitive column names (as would be SQL standard anyways).Retrogression
I can confirm this to be the case also. It's SUCH a shame for Amazon to NOT mention case at all in their docs and a HUGE miss for the Redshift "Auto" copy. Essentially, if your JSON property names use anything other than lowercase characters, you must use a JSONPaths file!Ruffner
There is an now an option to ignore case when loading JSON data to Redshift from s3Pewter
N
3

COPY maps the data elements in the JSON source data to the columns in the target table by matching object keys, or names, in the source name/value pairs to the names of columns in the target table. The matching is case-sensitive. Column names in Amazon Redshift tables are always lowercase, so when you use the ‘auto’ option, matching JSON field names must also be lowercase. If the JSON field name keys aren't all lowercase, you can use a JSONPaths file to explicitly map column names to JSON field name keys.

The solution would be to use jsonpath

Example json:

{
"Name": "Major",
"Age": 19,
"Add": {
"street":{
"st":"5 maint st",
"ci":"Dub"
},
"city":"Dublin"
},

"Category_Name": ["MLB","GBM"]

}

Example table:

(
name varchar,
age int,
address varchar,
catname varchar
);

Example jsonpath:

{
"jsonpaths": [
"$['Name']",
"$['Age']",
"$['Add']",
"$['Category_Name']"
]
}

Example copy code:

copy customer --redshift code
from 's3://mybucket/customer.json'
iam_role 'arn:aws:iam::0123456789012:role/MyRedshiftRole'
json from 's3://mybucket/jpath.json' ; -- Jsonpath file to map fields

Examples are taken from here

Nardone answered 16/10, 2019 at 10:35 Comment(0)
C
1

this could be because the column names of the redshift table are in lower case and the column names in JSON files are in upper(or camel case). as a workaround we can use 'auto ignorecase' instead of 'auto' option and redshift tries to match the corresponding columns. https://docs.aws.amazon.com/en_us/redshift/latest/dg/copy-parameters-data-format.html#copy-json

the information is mentioned in the copy parameters section.

Cheiro answered 7/7, 2021 at 6:47 Comment(0)
P
1

There is an now an option to ignore case when loading json data to Redshift from s3

COPY crypt.public.coindetails FROM 's3://cryptstreaxxxx/filetest2.json'
IAM_ROLE 'arn:aws:iam::xxxxxxx:role/service-role/AmazonRedshift-CommandsAccessRole-20211201T210748' 
FORMAT AS JSON 'auto ignorecase' REGION AS 'us-east-1'

in the Redshift UI click on File Options and choose the option as shown in the image below

enter image description here

Pewter answered 2/12, 2021 at 0:25 Comment(0)
G
0

For cases when JSON data objects don't correspond directly to column names you can use a JSONPaths file to map the JSON elements to columns as mentioned by TimZ and described here

Grijalva answered 7/11, 2017 at 21:27 Comment(0)
B
0

Reporting my experience as might be useful for someone else.

In my case I was loading data with INSERT statement and I had camel-case fields as well. When I tried to query fields of the JSON column I got null as a result.

So I had to load the specific field as

INSERT INTO my_schema.my_table (
    SELECT json_parse(lower(my_json),
    [...]
    FROM [...]
);

After lower casing the source JSON field I was able to correctly query the JSON fields.

https://docs.aws.amazon.com/redshift/latest/dg/JSON_PARSE.html https://docs.aws.amazon.com/redshift/latest/dg/r_LOWER.html

Beret answered 31/1, 2022 at 15:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.