Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare JSON values in MariaDB

Tags:

json

mariadb

How can I compare two JSON values in MariaDB? Two values such as {"b": 1, "a": 2} and {"a": 2, "b": 1} should be equal. Does MariaDB contain function to reorder elements of a JSON value?

like image 754
Петр Александров Avatar asked Jan 23 '26 08:01

Петр Александров


1 Answers

If you expect to need this (uncommon) kind of comparison, build the JSON in some canonical way before storing it. The obvious way for a simple JSON like yours is to alphabetize the keys. How to do that will depend on the "encode" library you are using for JSON.

like image 172
Rick James Avatar answered Jan 26 '26 01:01

Rick James