[MDEV-14778] NULL is a valid JSON document Created: 2017-12-27  Updated: 2017-12-27  Resolved: 2017-12-27

Status: Closed
Project: MariaDB Server
Component/s: JSON
Affects Version/s: 10.2.11
Fix Version/s: N/A

Type: Bug Priority: Major
Reporter: richardeaxon Assignee: Sergei Golubchik
Resolution: Not a Bug Votes: 0
Labels: None
Environment:

Centos 7



 Description   

Using the following test table:

CREATE TABLE `test` (
	`extra_info` BLOB NULL DEFAULT NULL
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
;
 
INSERT INTO `test` VALUES (NULL, 1);

And then running the following queries:

SELECT json_valid(null);      # returns null, should return 1?
SELECT json_valid('{}');        # returns 1
 
UPDATE test
SET extra_info = JSON_SET(extra_info, '$.service_supplier_id', 999)
WHERE test_int = 1;

I expect a new JSON document to be created as per JSON lint and RFC 7159 as per the following URL's as NULL is a valid JSON document.

https://stackoverflow.com/questions/8526995/is-null-valid-json-4-bytes-nothing-else
https://jsonlint.com/

This goes against the MariaDB documentation that states NULL will result in a NULL document:

Updates or inserts data into a JSON document, returning the result, or NULL if any of the arguments are NULL or the optional path fails to find an object.

I think MariaDB implements an older JSON standard preventing creation of a new document into a NULL column. Or is there another way to achieve the above JSON_SET to create a new document?



 Comments   
Comment by richardeaxon [ 2017-12-27 ]

Workaround is:

UPDATE test
SET extra_info = JSON_SET(ifnull(extra_info, '{}'), '$.service_supplier_id', 999)
WHERE test_int = 1;

Comment by Sergei Golubchik [ 2017-12-27 ]

the first link says

The literal token null is a JSON value

But SQL NULL is not a "literal token null", it's an absence of value. See

MariaDB [test]> SELECT json_valid(null);
+------------------+
| json_valid(null) |
+------------------+
|             NULL |
+------------------+
1 row in set (0.00 sec)
 
MariaDB [test]> SELECT json_valid('null');
+--------------------+
| json_valid('null') |
+--------------------+
|                  1 |
+--------------------+
1 row in set (0.00 sec)

Generated at Thu Feb 08 08:16:12 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.