- Newest
- Most votes
- Most comments
try this
SELECT
userIdentity.username as username,
eventtime,
eventname,
awsregion,
sourceipaddress,
useragent,
requestparameters,
CAST(json_extract(json_parse(requestparameters), '$["x-amz-copy-source"]') AS VARCHAR) as source,
CAST(json_extract(json_parse(requestparameters), '$.bucketName') AS VARCHAR) as bucketName,
CAST(json_extract(json_parse(requestparameters), '$.key') AS VARCHAR) as targetObject
FROM table
WHERE eventName = 'CopyObject'
ORDER BY eventtime desc;
Regards, Andrii
Hello.
The error you're receiving, INVALID_FUNCTION_ARGUMENT: Invalid JSON path, indicates that there is an issue with the JSON path you've provided. In Athena, which is based on Presto, the json_extract_scalar function is used to extract a scalar value from a JSON object using the provided JSON path.
The problem may arise from the fact that the - character has special meaning in JSON paths. To access properties with - in their names, you often need to use double quotes around the property name.
Try modifying your JSON path for 'x-amz-copy-source' as follows:
json_extract_scalar(requestparameters, '$."x-amz-copy-source"') as source
Regards, Andrii
Yes, for SQL language, the approach I mentioned above is incorrect, and I have also tried your approach, which doesn't work, it also notice ' INVALID_FUNCTION_ARGUMENT: Invalid JSON path: '$."x-amz-copy-source"' '. Additionally, I can assure that 'requestparameters' has the property 'x-amz-copy-source'. Thanks
In some SQL engines, JSON paths need to be structured differently. If you're trying to access a JSON property in Redshift, the format should be something like json_extract_path_text(requestparameters, 'x-amz-copy-source'). Ensure the column containing the JSON data is indeed named 'requestparameters'.
Ensure that all rows in your requestparameters column are valid JSON. If even a single row contains invalid JSON, it could cause the entire query to fail. You can identify invalid JSON rows using a query like:
WHERE NOT is_valid_json(requestparameters)
Regards, Andrii
Hello, It still doesn't work. Athena is different from regular SQL; it doesn't support many commonly used syntaxes. Thanks
Sorry, it`s an answer to another question. Could you try this solution:
Instead of directly using json_extract_scalar, first, parse the JSON string using json_parse and then use the ->> operator to extract the value.
SELECT
userIdentity.username as username,
eventtime,
eventname,
awsregion,
sourceipaddress,
useragent,
requestparameters,
CAST(json_parse(requestparameters) ->> 'x-amz-copy-source' AS VARCHAR) as source,
CAST(json_parse(requestparameters) ->> 'bucketName' AS VARCHAR) as bucketName,
CAST(json_parse(requestparameters) ->> 'key' AS VARCHAR) as targetObject
FROM table
WHERE eventName = 'CopyObject'
ORDER BY eventtime desc;
This way, we first parse the JSON string into a JSON object, and then use the ->> operator to get the value corresponding to a specific key, and finally cast the result as VARCHAR (or whatever appropriate type). This should help bypass issues with json_extract_scalar.
Regards, Andrii
it still not work, it notice ' line 9:40: mismatched input '->'. Expecting: '.', 'AS', 'FILTER', 'IGNORE', 'OVER', 'RESPECT', '[' ' it not support '->>'
Thanks
Relevant content
- asked 7 months ago
- asked 2 years ago
- Why is no data migrated from my Amazon S3 source endpoint even though my AWS DMS task is successful?AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 10 months ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 2 years ago
Success Thank you so much