简体   繁体   中英

Sql query for data stored as JSON

I have data stored in column as JSON, as shown below:

{"category":{"value":CAT, "demo":A.......

I want to query in SSMS and want an output like:

CAT

I tried "SELECT JSON_QUERY(TABLENAME,'$.CATEGORY') FROM TABLENAME" which gave result of the

{"value":CAT, "demo":A....... but I want only CAT. How do I do that?

Use JSON_VALUE to get specific values.

SELECT JSON_VALUE(TABLENAME,'$.CATEGORY.VALUE') AS Value

JSON_QUERY is for JSON fragments.

Source

You should use JSON_VALUE.

SELECT JSON_VALUE(ColumnName, '$.category.value') FROM TableName

Take note of the first parameter, it should be the column name, not the table name (as opposed to your example).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM