5

Let's say we have this json in our database table. I want to select value from tags. I already know how to get an array from this data but I don't know how to access array members. The question would be how do I get the first value from the array? Is there a function for this task?

{
    "info": {
        "type": 1,
        "address": {
            "town": "Bristol",
            "county": "Avon",
            "country": "England"
        },
        "tags": ["Sport", "Water polo"]
    },
    "type": "Basic"
} 

Query I already have:

SELECT JSON_QUERY(MyTable.Data, '$.info.tags')
FROM MyTable

This returns me:

["Sport", "Water polo"]

How do I get

Sport

1 Answer 1

6

JSON_QUERY returns an object or array. You need JSON_VALUE to return a scalar value, eg :

SELECT JSON_VALUE(Data, '$.info.tags[0]')
from MyTable

Check the section Compare JSON_VALUE and JSON_QUERY in the docs for more examples

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.