英文:
Snowflake Response Translator - Uncaught SyntaxError: "[object Object]" is not valid JSON
问题
You are getting this error because the response from your external function test
is not valid JSON, but the response_translator
function expects valid JSON as input.
The response from your test
function appears to contain HTML-encoded characters, such as "
for double quotes, which are not valid JSON. When the response_translator
function tries to parse this response, it encounters the [object Object]
string, which is not a valid JSON object, hence the error.
To resolve this issue, you should ensure that the response from your test
function returns valid JSON without HTML-encoded characters. You may need to modify the external service or API integration so that it returns properly formatted JSON.
If you have control over the external service, you should ensure that it returns JSON like this:
{
"statusCode": 200,
"headers": null,
"multiValueHeaders": null,
"body": "{\"error\":{\"grpc_code\":5,\"http_code\":404,\"message\":\"Resource not found\",\"http_status\":\"Not Found\"}}"
}
This way, the response_translator
function can parse the response correctly.
英文:
My external function looks like this:
create or replace external function test(value string)
returns variant
RETURNS NULL ON NULL INPUT
api_integration = my_api
response_translator = TEST_SNOWFLAKE.PUBLIC.response_translator
as 'https://abc'
;
My response translator:
CREATE OR REPLACE FUNCTION response_translator(EVENT OBJECT)
RETURNS OBJECT
LANGUAGE JAVASCRIPT AS
'
const parsedJson = JSON.parse(EVENT.body);
const parsedJsonString = JSON.stringify(parsedJson);
const modifiedJson = {
"data": [
[0, parsedJsonString]
]
};
return modifiedJson;
';
When using this test:
select response_translator(parse_json(' {"body": "{\\"error\\":{\\"grpc_code\\":5,\\"http_code\\":404,\\"message\\":\\"Resource not found\\",\\"http_status\\":\\"Not Found\\"}}"
}'));
the response translator works fine.
However, when triggering the external function with:
select test('abc123');
which has the following response as seen in the AWS Lambda logs:
{
"statusCode": 200,
"headers": null,
"multiValueHeaders": null,
"body": "{\"error\":{\"grpc_code\":5,\"http_code\":404,\"message\":\"Resource not found\",\"http_status\":\"Not Found\"}}"
}
I get this error in my snowflake console:
JavaScript execution error: Uncaught SyntaxError: "[object Object]" is not valid JSON in RESPONSE_TRANSLATOR at '[object Object]' position 1
Why am I getting this error?
答案1
得分: 1
这个响应翻译器已经正常运行:
CREATE OR REPLACE FUNCTION response_translator(EVENT OBJECT)
RETURNS OBJECT
LANGUAGE JAVASCRIPT AS
'
if (EVENT.body.error!=null){
const modifiedJson = {"body":{
"data": [
[0, EVENT.body]
]
}};
return modifiedJson;}
';
英文:
This response translator got it working:
CREATE OR REPLACE FUNCTION response_translator(EVENT OBJECT)
RETURNS OBJECT
LANGUAGE JAVASCRIPT AS
'
if (EVENT.body.error!=null){
const modifiedJson = {"body":{
"data": [
[0, EVENT.body]
]
}};
return modifiedJson;}
';
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论