I have JSON data like this saved in S3. I am using ATHENA to write select statements.
{
"sample_data":{
"people":[
{
"firstName":"Emily",
"address":{
"streetAddress":"101",
"city":"abc",
"state":"",
"phoneNumbers":[
{
"type":"home",
"number":"3"
},
{
"type":"city",
"number":"4"
}
]
}
},
{
"firstName":"Smily",
"address":{
"streetAddress":"102",
"city":"def",
"state":"",
"phoneNumbers":[
{
"type":"home",
"number":"1"
},
{
"type":"city",
"number":"1"
}
]
}
}
]
}
}
How Can I write a select statement that selects streetaddress and city where home>2 and city=4;
I tried UNNEST but that did not help.
Expected Output:
streetAddress city
101 abc
Tried this UNNEST but it extracted phoneNumbers to multiple rows. So can not query by both home and city as they are in different rows now.
SELECT idx,JSON_EXTRACT_SCALAR(x.n, '$.address.streetaddress') as streetaddress,
JSON_EXTRACT_SCALAR(x.n, '$.address.city') as city, JSON_EXTRACT_SCALAR(x.m, '$.type') as type, JSON_EXTRACT_SCALAR(x.m, '$.number') as value
FROM sample_data1 cross join
UNNEST (CAST(JSON_EXTRACT(sample_data,'$.people') AS ARRAY<JSON>)) AS x(n)
CROSS JOIN
UNNEST (CAST(JSON_EXTRACT(x.n,'$.address.phonenumbers') AS ARRAY<JSON>)) WITH ordinality AS x(m,idx) ;