2016-04-25 13 views
1

AWS Redshiftには、各レコードのキーと値のペアが異なるJSON文字列からすべてのキーと値のペアをクエリする方法はありますか?AWS RedshiftでさまざまなJSON文字列をクエリ

I.e.以下の例では、最初の動物は 'location'属性を持ち、2番目の動物は 'location'属性を持っています。私が知っている属性が両方のレコードのために存在して選ぶことができますjson_extract_path_text機能を使用するが、それは失敗する場所を照会しようとすると:

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; 

ERROR: 42601: syntax error at or near "location"

望ましい結果:

そのjson_extract_path_textことがわかっ
animal_id name animal_type age location 
1 harry cat 2 oakland 
2 louie dog 4 NULL 
+0

余分な '、'の後の場所 –

+0

@vkpごめんなさい更新されたクエリ。たとえ後続のカンマがなくても同じエラーメッセージが表示されます – fez

答えて

0

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; 
関連する問題