{"id":474,"date":"2016-09-08T09:22:40","date_gmt":"2016-09-08T14:22:40","guid":{"rendered":"http:\/\/thenoyes.com\/littlenoise\/?p=474"},"modified":"2016-09-22T14:18:53","modified_gmt":"2016-09-22T19:18:53","slug":"aggregate-json-function","status":"publish","type":"post","link":"https:\/\/thenoyes.com\/littlenoise\/?p=474","title":{"rendered":"Aggregate JSON function in MySQL"},"content":{"rendered":"<p>There is not yet an equivalent to GROUP_CONCAT that produces a JSON array. (There is in <a href=\"http:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/json-creation-functions.html#function_json-arrayagg\">MySQL 8<\/a>, but that&#8217;s not GA yet.) Until then, you can hack it together with string functions:<\/p>\n<pre>SELECT * FROM t;\r\n+------+--------+\r\n| id   | data   |\r\n+------+--------+\r\n|    1 | First  |\r\n|    2 | Second |\r\n+------+--------+\r\n\r\nSELECT CONCAT('[', GROUP_CONCAT(JSON_OBJECT('id', id, 'value', data) SEPARATOR ', '), ']') AS j FROM t;\r\n+-------------------------------------------------------------+\r\n| j                                                           |\r\n+-------------------------------------------------------------+\r\n| [{\"id\": 1, \"value\": \"First\"}, {\"id\": 2, \"value\": \"Second\"}] |\r\n+-------------------------------------------------------------+\r\n<\/pre>\n<p>Or you can use all JSON functions but hack the grouping:<\/p>\n<pre>SELECT j FROM (\r\n       SELECT\r\n         @c := @c + 1 AS c,\r\n         @j := JSON_MERGE(@j, JSON_OBJECT('id', id, 'value', data)) AS j\r\n       FROM t\r\n       JOIN (SELECT @c := 0,  @j := JSON_ARRAY()) dt1\r\n     ) dt2 ORDER BY c DESC LIMIT 1;\r\n+-------------------------------------------------------------+\r\n| j                                                           |\r\n+-------------------------------------------------------------+\r\n| [{\"id\": 1, \"value\": \"First\"}, {\"id\": 2, \"value\": \"Second\"}] |\r\n+-------------------------------------------------------------+\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>There is not yet an equivalent to GROUP_CONCAT that produces a JSON array. (There is in MySQL 8, but that&#8217;s not GA yet.) Until then, you can hack it together with string functions: SELECT * FROM t; +&#8212;&#8212;+&#8212;&#8212;&#8211;+ | id | data | +&#8212;&#8212;+&#8212;&#8212;&#8211;+ | 1 | First | | 2 | Second | +&#8212;&#8212;+&#8212;&#8212;&#8211;+ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[8],"tags":[],"class_list":["post-474","post","type-post","status-publish","format-standard","hentry","category-mysql-faq"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2IBF1-7E","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/474","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=474"}],"version-history":[{"count":7,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/474\/revisions"}],"predecessor-version":[{"id":490,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/474\/revisions\/490"}],"wp:attachment":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}