In-depth understanding of JSON_ARRAYAGG and JSON_OBJECT functions in MySQL

In MySQL database, data processing in JSON format has become more and more common. JSON (JavaScript Object Notation) is a lightweight data exchange format that can be used to store and represent structured data. MySQL provides some powerful JSON functions, two of the key functions are JSON_ARRAYAGG and JSON_OBJECT. This article will dive into the purpose, syntax, and examples of these two functions to help you better understand their functionality and usage.

JSON_ARRAYAGG function

JSON_ARRAYAGGFunction is used to combine multiple rows from the query results into a JSON array. This is useful for summarizing data from multiple rows in one query. The following is the basic syntax of the JSON_ARRAYAGG function:

JSON_ARRAYAGG(expression)
  • expression: The expression or column to be included in the array.

Example

We can use the JSON_ARRAYAGG function to merge all the province names in the com_province table into a JSON array:

# 查询省份表中所有的省份名称的JSON数组
select JSON_ARRAYAGG(province_name ) from com_province;

This will return a JSON array containing 31 provinces:

["北京", "天津", "河北省", "山西省", "内蒙古自治区", "辽宁省", "吉林省", "黑龙江省", "上海", "江苏省", "浙江省", "安徽省", "福建省", "江西省", "山东省", "河南省", "湖北省", "湖南省", "广东省", "广西壮族自治区", "海南省", "重庆", "四川省", "贵州省", "云南省", "西藏自治区", "陕西省", "甘肃省", "青海省", "宁夏回族自治区", "新疆维吾尔自治区"]

JSON_OBJECT function

The JSON_OBJECT function is used to create a JSON object containing specified key-value pairs. This is useful for generating JSON objects containing specific data. The following is the basic syntax of the JSON_OBJECT function:

JSON_OBJECT(key1, value1, key2, value2, ...)
  • key1, value1, key2, value2, ...: key-value pairs, the key is a string, and the value can be any valid expression.

Example:

Use the JSON_OBJECT function to create a JSON object containing the province name and province code.

# 使用JSON_OBJECT函数创建一个包含省份名称和省份编码的JSON对象
select JSON_OBJECT('省份名称',province_name,'省份编码',province_code ) from com_province;

_20230911225815.png

Practical examples

We have three tables, which store province information, city information and administrative district information respectively. Now we need to provide the client with json data of provinces and cities. We can use JSON_ARRAYAGGand JSON_OBJECTto complete:

SELECT
	JSON_ARRAYAGG(
		JSON_OBJECT(
			'code',
			T1.province_code,
			'name',
			T1.province_name,

			'children',
			(
			SELECT
				JSON_ARRAYAGG( JSON_OBJECT( 'code', T2.city_code, 'name', T2.city_name,'children',
					(
			SELECT
				JSON_ARRAYAGG( JSON_OBJECT( 'code', T3.area_code, 'name', T3.area_name ) ) AS test3 
			FROM
				com_area T3 
			WHERE
				T3.city_code = T2.city_code ORDER BY T3.sort asc
			) 
			) ) AS test2 
			FROM
				com_city T2 
			WHERE
				T2.province_code = T1.province_code  ORDER BY T2.sort asc
			) 
		) 
	) AS test 
FROM
	com_province T1   ORDER BY  T1.sort asc

result:

_20230911231557.png

in conclusion

JSON_ARRAYAGGand JSON_OBJECTare powerful tools in MySQL for processing JSON data. JSON_ARRAYAGGUsed to merge data from multiple rows into a JSON array, and JSON_OBJECTused to create a JSON object containing key-value pairs. These functions help you manipulate and query JSON data in your database more efficiently to meet a variety of needs. Whether you are developing web applications or performing data analysis, knowing how to use these functions will be very helpful in your work. I hope this article will help you gain a deeper understanding of JSON_ARRAYAGGand JSON_OBJECTfunctions.

Guess you like

Origin blog.csdn.net/weixin_44002151/article/details/132819678