Requirements Description :
With the Json_merge function in MySQL, you can combine multiple JSON objects into one object
Operation Process :
1. View a table with JSON columns
Mysql> SELECT * from tab_json;+----+--------------------------------------------------------------------------- --------+| ID | Data |+----+--------------------------------- --------------------------------------------------+| 1 | {"Tel": "132223232444", "name": "David", "Address": "Beijing"} | | 2 | {"Tel": "13390989765", "name": "Mike", "Address": "Guangzhou"} | | 3 | {"Names": "Smith"} | | 4 | {"Names": "Smith", "Address": "Beijing"} | | 5 | {"Names": "Smith", "Address": "Beijing", "Birthday": "2018-09-09"} | | 6 | {"Max": "true", "names": "Smith", "Address": "Beijing", "Birthday": "2018-09-09"} | | 7 | {"Max": "True", "names": "Smith", "Address": "Beijing", "Birthday": "2018-09-09"} | | 8 | {"Oax": "true", "names": "Smith", "Address": "Beijing", "BirThday ":" 2018-09-09 "} |+----+----------------------------------------------------------------------------------- +8 rows in Set (0.00 sec)
2. Merges the value of names with the value of address
Mysql> Select Json_extract (data, ' $.names '), json_extract (data, ' $.address ') from tab_json;+---------------------- --------+--------------------------------+| Json_extract (data, ' $.names ') | Json_extract (data, ' $.address ') |+------------------------------+--------------------------------+| NULL | "Beijing" | | NULL | "Guangzhou" | | "Smith" | NULL | | "Smith" | "Beijing" | | "Smith" | "Beijing" | | "Smith" | "Beijing" | | "Smith" | "Beijing" | | "Smith" | "Beijing" |+------------------------------+--------------------------------+8 rows in Set (0.00 sec) m Ysql> SelectJson_merge(Json_extract (data, ' $.names '), json_extract (data, ' $.address ')) from tab_json;+----------------------------------- --------------------------------------+| Json_merge (json_extract (data, ' $.names '), json_extract (data, ' $.address ')) |+------------------------------------- ------------------------------------+| NULL | | NULL | | NULL | | ["Smith", "Beijing"] | | ["Smith", "Beijing"] | | ["Smith", "Beijing"] | | ["Smith", "Beijing"] | | ["Smith", "Beijing"] |+------------------------------------------------ -------------------------+8 rows in Set (0.00 sec)
3. If multiple objects contain the same key, they are also merged into a specific values
mysql> SELECT json_merge ('{"a": 1, "B": 2}', '"a": 4}[1, 4], "B": 2, "C": 3} |+----------------------------------------------------+1 row in Set (0.00 sec)
Note: Combining the values of two objects into one, the value of a key also increases to 2.
Document created: June 6, 2018 17:49:18
What is the use of Json_merge functions in MySQL?