Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query to determine if a JSON value contains a specified attribute

Tags:

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:

  • Determine which rows have a "cost" attribute (items 1 & 3)
  • Determine which rows do not have a "cost" attribute (items 2 & 4)
  • Determine which rows have a value set for the "cost" attribute (item 1)

Thank you! This is my first time asking (at least recently) so any help is greatly appreciated!

like image 268
Matthew J Gravelyn Avatar asked Sep 27 '16 18:09

Matthew J Gravelyn


People also ask

How can I get specific data from JSON in SQL?

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).

Can I query a JSON?

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.

Is JSON check in SQL?

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.

What is JSON extract () function in MySQL?

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.


2 Answers

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.

  1. WHERE JSON_EXTRACT(colname, '$.cost') IS NOT NULL
  2. WHERE JSON_EXTRACT(colname, '$.cost') IS NULL
  3. 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

like image 149
Barmar Avatar answered Oct 07 '22 18:10

Barmar


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.

like image 45
Peter Matisko Avatar answered Oct 07 '22 18:10

Peter Matisko