Sql 在AWS红移中查询不同的JSON字符串

Sql 在AWS红移中查询不同的JSON字符串,sql,json,amazon-web-services,amazon-redshift,Sql,Json,Amazon Web Services,Amazon Redshift,AWS Redshift中是否有方法从JSON字符串中查询所有键/值对,其中每个记录都有不同数量的键/值对 即,在下面的示例中,第一种动物具有“位置”属性,而第二种动物没有。使用该函数,我可以为两条记录选择我知道存在的属性,但在尝试查询位置时,它将失败: create table test.jsondata(json varchar(65000)); insert into test.jsondata(json) values ('{ "animal_id": 1, "name"

AWS Redshift中是否有方法从JSON字符串中查询所有键/值对,其中每个记录都有不同数量的键/值对

即,在下面的示例中,第一种动物具有“位置”属性,而第二种动物没有。使用该函数,我可以为两条记录选择我知道存在的属性,但在尝试查询位置时,它将失败:

   create table test.jsondata(json varchar(65000));
    insert into  test.jsondata(json) values ('{ "animal_id": 1, "name": "harry", "animal_type": "cat", "age": 2, "location": "oakland"}');
    insert into  test.jsondata(json) values ('{ "animal_id": 2, "name": "louie","animal_type": "dog", "age": 4}');

    select 
      json_extract_path_text(JSON,'animal_id') animal_id,
      json_extract_path_text(JSON,'name') name,
      json_extract_path_text(JSON,'animal_type') animal_type,
      json_extract_path_text(JSON,'age') age,
      json_extract_path_text(JSON,'location') location
    from test.jsondata
    order by animal_id;
错误:42601:在“位置”处或附近出现语法错误

预期结果:

animal_id name animal_type age location
1 harry cat 2 oakland
2 louie dog 4 NULL

发现json_extract_path_文本可与
ISNULL
一起使用:

select 
      json_extract_path_text(JSON,'animal_id') animal_id,
      json_extract_path_text(JSON,'name') name,
      json_extract_path_text(JSON,'animal_type') animal_type,
      json_extract_path_text(JSON,'age') age,
      ISNULL(json_extract_path_text(JSON,'location'),NULL) location
    from test.jsondata
    order by animal_id; 

发现json_extract_path_文本可与
ISNULL
一起使用:

select 
      json_extract_path_text(JSON,'animal_id') animal_id,
      json_extract_path_text(JSON,'name') name,
      json_extract_path_text(JSON,'animal_type') animal_type,
      json_extract_path_text(JSON,'age') age,
      ISNULL(json_extract_path_text(JSON,'location'),NULL) location
    from test.jsondata
    order by animal_id; 

额外的
之后location@vkp抱歉,是更新的查询。即使没有后面的逗号,我也会收到相同的错误消息extra
location@vkp抱歉,是更新的查询。即使没有后面的逗号,我也会收到相同的错误消息