0

MySQL since version 5.7 has some fundamental JSON support, however I've been looking through the documentation but couldn't find anything JSON aggregatable functions.

Are there any JSON aggregate functions in MySQL 5.7+?

For example if you have query:

SELECT id, GROUP_CONCAT(name) FROM given_names GROUP BY id

You'd get a result like:

id | name
1  | Jon,Smith
2  | Hubert,Blaine,Wolfeschlegelsteinhausenbergerdorff
....

Is there an equivalent way to get a JSON array with?

My concern is with properly escaping strings because when you use GROUP_CONCAT if the string contains the separator it is near impossible to differentiate the parts afterwards.

2
  • The JSON functions seem so antithetical to the idea of relational data, I just can't figure out why they're there. What's wrong with spitting sql out to json_encode? Commented Jan 27, 2017 at 13:48
  • @Strawberry I'm not arguing for JSON functions in a relational database, although I think MySQL is just trying to invade the NoSQL landscape by becoming "relational and then some" but I'm just saying, since JSON is already in there, why not JSON aggregation. The reason I ask is that I could eliminate a lot of rows with replicated data if such a thing existed (in this example it would just be id but there could be more). Commented Jan 27, 2017 at 14:21

1 Answer 1

2

As of MySQL 5.7.22 it is possible using either JSON_ARRAYAGG and JSON_OBJECTAGG. Examples taken from the documentation:

JSON_ARRAYAGG

mysql> SELECT o_id, attribute, value FROM t3;
+------+-----------+--------+
| o_id | attribute | value  |
+------+-----------+--------+
|    2 | color     | red    |
|    2 | fabric    | silk   |
|    3 | color     | green  |
|    3 | shape     | square |
+------+-----------+--------+
4 rows in set (0.00 sec)

mysql> SELECT o_id, JSON_ARRAYAGG(attribute) AS attributes 
       FROM t3 GROUP BY o_id;
+------+---------------------+
| o_id | attributes          |
+------+---------------------+
|    2 | ["color", "fabric"] |
|    3 | ["color", "shape"]  |
+------+---------------------+
2 rows in set (0.00 sec)

JSON_OBJECTAGG

mysql> SELECT o_id, attribute, value FROM t3;
+------+-----------+--------+
| o_id | attribute | value  |
+------+-----------+--------+
|    2 | color     | red    |
|    2 | fabric    | silk   |
|    3 | color     | green  |
|    3 | shape     | square |
+------+-----------+--------+
4 rows in set (0.00 sec)

mysql> SELECT o_id, JSON_OBJECTAGG(attribute, value)
       FROM t3 GROUP BY o_id;
+------+---------------------------------------+
| o_id | JSON_OBJECTAGG(attribute, value)      |
+------+---------------------------------------+
|    2 | {"color": "red", "fabric": "silk"}    |
|    3 | {"color": "green", "shape": "square"} |
+------+---------------------------------------+
2 rows in set (0.00 sec)
Sign up to request clarification or add additional context in comments.

1 Comment

Those functions are also in MySQL 5.7.22. Cf. dev.mysql.com/doc/refman/5.7/en/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.