I have a table with a column for storing a JSON value for each row. Currently, these are not standardized. I'd like to end up with each of these JSON values being standardized in number and title of attributes. Is there a query I can use to determine whether or not each JSON value contains a specified attribute?
As an example, here are what some of the JSON values look like:
{"name":"Item 1","cost":"4.99"} {"name":"Item 2"} {"name":"Item 3","cost":""} {"name":"Item 4"}
How do I:
Thank you! This is my first time asking (at least recently) so any help is greatly appreciated!
To query JSON data, you can use standard T-SQL. If you must create a query or report on JSON data, you can easily convert JSON data to rows and columns by calling the OPENJSON rowset function. For more information, see Convert JSON Data to Rows and Columns with OPENJSON (SQL Server).
You can query JSON data using a simple dot notation or, for more functionality, using SQL/JSON functions and conditions. You can create and query a data guide that summarizes the structure and type information of a set of JSON documents.
ISJSON Function in Sql Server 2016ISJSON function validates whether the string parameter supplied to it is a valid JSON or not. If supplied string is a valid JSON then it will return value as 1, otherwise it returns the value 0. In case input is a NULL then it returns output as NULL.
In MySQL, the JSON_EXTRACT() function returns data from a JSON document. The actual data returned is determined by the path you provide as an argument. You provide the JSON document as the first argument, followed by the path of the data to return.
I assume you're using MySQL 5.7, which adds the JSON
data type. Use JSON_EXTRACT(colname, '$.cost')
to access the cost
property. It will be NULL
is there's no such property.
WHERE JSON_EXTRACT(colname, '$.cost') IS NOT NULL
WHERE JSON_EXTRACT(colname, '$.cost') IS NULL
WHERE JSON_EXTRACT(colname, '$.cost') != ''
It will also be NULL
if the value in the JSON is null
; if you need to distinguish this case, see Can't detect null value from JSON_EXTRACT
If you need to only check, whether the json key exists, you can use JSON_CONTAINS_PATH. I expect it to be faster than JSON_EXTRACT.
mySQL 8.0 docs
Example:
I have a permission column in the DB. The structure looks like this:
{ "users": { "user1" : "rw", "user2" : "r" }, "groups": { "root": "rw", "anotherGroup" : "r" } }
If I want to get all the items, that have the root group (regarding of the actual permissions), I use this:
SELECT * FROM `filesystem_folders` where JSON_CONTAINS_PATH(permissions, 'one', '$.groups.root');
I made just a very simple time comparison between the select above which took 12.90s (10000 reads). Vs the following select using JSON_EXTRACT
SELECT * FROM `filesystem_folders` where JSON_EXTRACT(permissions, '$.groups.root') IS NOT NULL;
which took 15.13s. So, quite a small difference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With