1

I have sample data like below, I want to display all project names, and not just one by passing index because the size of array is not known in every column.

By trying below SQL getting only 'project1'. Need both project1 and project2 in concatenation.

WITH dataset AS 
(
   SELECT '{"name": "Bob Smith",
             "org": "engineering",
             "projects": [{"name":"project1", "completed":false},{"name":"project2", "completed":true}]}'
     AS myblob
)
SELECT json_extract_scalar(myblob, '$.projects[0].name') AS project_name
FROM dataset

Expecting like both project1 and project2 in concatenation.

project_name
project1,project2

1 Answer 1

3

There are multiple options. For example you can use the json_query which has better JSON path support than json_extract:

SELECT json_query(myblob, 'lax $.projects[*].name' WITH  ARRAY WRAPPER) projects
FROM dataset;

Output:

        projects
-------------------------
 ["project1","project2"]
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much!!. But When Im trying with data getting this error- Cannot read input of type json as JSON using formatting JSON. Thanks in advance.
@PavanAithal This query worked for me on the provided sample data. Note that unlike json_extract json_query can't work with JSON type, only with VARCHAR. You can try calling json_format on your column first.

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.