Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying JSON array in Redshift?

Are you able to query a field within an array in a Redshift JSON column?

I have the following JSON:

{"sort_details":[{"sort_by":"name","order":"asc"}]}

Is it possible to query anything lower than the highest level element in Redshift? I've tried using

json_extract_path_text( myjson , 'sort_details' , 'sort_by' ) 

but got a null row back. I'm guessing that being an array and conceivably returning multiple results per record, this might not be possible.

like image 454
Todd Avatar asked Sep 26 '26 21:09

Todd


2 Answers

You could use nested JSON functions:

json_extract_path_text(
    json_extract_array_element_text(
        json_extract_path_text( 
            myjson, 
            'sort_details'
        ), 
        0
    ), 
    'sort_by'
)
like image 160
Jared Piedt Avatar answered Sep 29 '26 17:09

Jared Piedt


JSON_EXTRACT_PATH_TEXT(myjson, 'sort_details', 0, 'sort_by')

This also works, but it's not documented in AWS Doc

like image 23
Nick Liu Avatar answered Sep 29 '26 18:09

Nick Liu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!