Athena: how to get information from json_array

0

If I want to extract specific properties from a JSON array using SQL, for example, if the requestparameters structure is as follows:

{
  'gree': [
    {
      'Grantee': {
        'xsi:type': 'CanonicalUser',
        'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
        'ID': 'xx'
      },
      'Permission': 'READ'
    },
    {
      'Grantee': {
        'xsi:type': 'CanonicalUser',
        'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
        'ID': 'xx'
      },
      'Permission': 'WRITE'
    },
    {
      'Grantee': {
        'xsi:type': 'CanonicalUser',
        'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
        'ID': 'xx'
      },
      'Permission': 'WRITE_ACP'
    }
  ]
}

If i were using MySQL, you could use the following syntax to extract all the ID properties and Permission properties:

SELECT
  json_extract(requestparameters, '$.gree[*].Grantee.ID') as ID,
  json_extract(requestparameters, '$.gree[*].Permission') as Permission
FROM table;

However, using [ * ] in this way is not valid syntax for Athena and would result in an error like 'INVALID_FUNCTION_ARGUMENT: Invalid JSON path: '$.AccessControlPolicy.AccessControlList.Grant[ * ].Permission''. If you replace [ * ] with [0], you can extract information from the first element. The question is, how can you extract properties from all elements?"

chunhui
asked 7 months ago87 views
No Answers

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions