简体   繁体   中英

How get nested json value from the following table in aws athena

Here is our code

SELECT "count"(*) "count"
    , "httprequest"."country"
    , "httprequest"."headers"
FROM waf_logs_17
WHERE ("httprequest"."uri" LIKE '/login')
GROUP BY "httprequest"."clientip", "httprequest"."country","httprequest"."headers"
ORDER BY "count" limit 5

Result is as follow

count Country headers
1 US [{name=Host, value=app.onlinecheckwriter.com}, {name=X-Forwarded-For, value=75.113.195.00}, {name=X-Forwarded-Proto, value=https}]

Now question is how we can select the value of X-Forwarded-For under "httprequest"."headers"

Your data is not json, but array of rows, so you can process it using array functions :

-- sample data
WITH dataset (headers) AS (
    values (array[cast(row('X-Forwarded-For', '75.113.195.00') as ROW(name varchar, value varchar))] )
)

-- query
select 
    reduce(headers, NULL, (v, curr) -> if(curr.name = 'X-Forwarded-For', curr.value, v), v -> v)
from dataset

Output:

_col0
75.113.195.00

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM