JSON flattening in athena (converting all keys into columns)

23 Views Asked by At

I have data like below. And I want all keys to convert as columns (table structure consists of columns as same as json keys). I want all values to be reflected in rows.

WITH dataset AS (
   SELECT '{
      "id": 1,
      "name": "John Doe",
      "age": 30,
      "address": {
         "street": "123 Main St",
         "city": "Anytown",
         "state": "CA",
         "zipcode": "12345"
      },
      "contacts": [
         {
            "type": "email",
            "value": "[email protected]"
         },
         {
            "type": "phone",
            "value": "555-1234"
         }
      ],
      "orders": [
         {
            "orderId": "A123",
            "products": [
               {
                  "productId": "P001",
                  "name": "Product 1",
                  "quantity": 2
               },
               {
                  "productId": "P002",
                  "name": "Product 2",
                  "quantity": 1
               }
            ],
            "totalAmount": 150.99
         },
         {
            "orderId": "B456",
            "products": [
               {
                  "productId": "P003",
                  "name": "Product 3",
                  "quantity": 3
               }
            ],
            "totalAmount": 75.50
         }
      ]
   }' AS myblob
)

I'm expecting a result something like this:

+----+----------+-----+--------------+---------+-------+---------+---------------+---------------------+-----------+--------------+------------+---------------+----------+-------------+
| id | name     | age | street       | city    | state | zipcode | contact_type  | contact_value       | order_id  | product_id    | product_name | quantity      | totalAmount  |
+----+----------+-----+--------------+---------+-------+---------+---------------+---------------------+-----------+--------------+------------+---------------+--------------+
| 1  | John Doe | 30  | 123 Main St  | Anytown | CA    | 12345   | email         | [email protected] | A123      | P001         | Product 1   | 2             | 150.99       |
| 1  | John Doe | 30  | 123 Main St  | Anytown | CA    | 12345   | email         | [email protected] | A123      | P002         | Product 2   | 1             | 150.99       |
| 1  | John Doe | 30  | 123 Main St  | Anytown | CA    | 12345   | phone         | 555-1234            | B456      | P003         | Product 3   | 3             | 75.50        |
+----+----------+-----+--------------+---------+-------+---------+---------------+---------------------+-----------+--------------+------------+---------------+--------------+

I'm trying it with the UNNEST function, but I'm not able to get the result as expected.

0

There are 0 best solutions below