1

I'm using Presto and trying to extract all 'id' from 'source'='dd' from a nested json structure as following.

{
  "results": [
    {
      "docs": [
        {
          "id": "apple1",
          "source": "dd"
        },
        {
          "id": "apple2",
          "source": "aa"
        },
        {
          "id": "apple3",
          "source": "dd"
        }
      ],
      "group": 99806
    }
  ]
}

expected to extract the ids [apple1, apple3] into a column in Presto I am wondering what is the right way to achieve this in Presto Query?

1 Answer 1

2

If your data has a regular structure as in the example you posted, you can use a combination of parsing the value as JSON, casting it to a structured SQL type (array/map/row) and the using array processing functions to filter, transform and extract the elements you want:

WITH data(value) AS (VALUES '{
  "results": [
    {
      "docs": [
        {
          "id": "apple1",
          "source": "dd"
        },
        {
          "id": "apple2",
          "source": "aa"
        },
        {
          "id": "apple3",
          "source": "dd"
        }
      ],
      "group": 99806
    }
  ]
}'),
parsed(value) AS (
  SELECT cast(json_parse(value) AS row(results array(row(docs array(row(id varchar, source varchar)), "group" bigint))))
  FROM data
)
SELECT 
  transform(                                        -- extract the id from the resulting docs
    filter(                                         -- filter docs with source = 'dd'
        flatten(                                    -- flatten all docs arrays into a single doc array
             transform(value.results, r -> r.docs)  -- extract the docs arrays from the result array
        ),
        doc -> doc.source = 'dd'),
    doc -> doc.id)
FROM parsed

The query above produces:

      _col0
------------------
 [apple1, apple3]
(1 row)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.