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
已提問 7 個月前檢視次數 91 次
沒有答案

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南