I have a sql like this

SELECT project_group.id,name,code,JSON_ARRAYAGG(permission_tag) as permission FROM `project_group` RIGHT JOIN group_permission on group_permission.group_id = project_group.id WHERE project = '8' AND `project_group`.`deleted_at` IS NULL GROUP BY project_group.`code`

it return some rows like:

Gorm Parsing Stringarray returned by SQL

How can i scan JSON_ARRAYAGG col in my go code?

I wrote a demo like this:

type GroupResponse struct {
    ID int `json:"id"`
    Name string `json:"name"`
    Code string `json:"code"`
    Permission pq.StringArray `json:"permission"`
}
groups := GroupResponse{}
    fetch := db.MySQL.Model(&module.ProjectGroup{}).Select("project_group.id,name,code,JSON_ARRAYAGG(permission_tag) as permission")
    fetch = fetch.Joins("RIGHT JOIN group_permission on group_permission.group_id = project_group.id").Where("project = ?", project).Group("project_group.`code`")
    fetch.Scan(&groups)

But code was return an error like this

 sql: Scan error on column index 3, name "permission": pq: unable to parse array; expected '{' at offset 0

How to scan JSON_ARRAYAGG? Here is data example:

DROP TABLE IF EXISTS `group_permission`;
CREATE TABLE `group_permission`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `group_id` bigint(20) NOT NULL,
  `permission_tag` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of group_permission
-- ----------------------------
INSERT INTO `group_permission` VALUES (1, 1, 'base');
INSERT INTO `group_permission` VALUES (2, 4, 'group.w');
INSERT INTO `group_permission` VALUES (4, 4, 'group.r');
INSERT INTO `group_permission` VALUES (5, 4, 'project.r');
INSERT INTO `group_permission` VALUES (6, 5, 'project.r');

-- ----------------------------
-- Table structure for project_group
-- ----------------------------
DROP TABLE IF EXISTS `project_group`;
CREATE TABLE `project_group`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(40) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  `code` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  `project` bigint(20) NOT NULL,
  `created_at` datetime(3) NULL DEFAULT NULL,
  `updated_at` datetime(3) NULL DEFAULT NULL,
  `deleted_at` datetime(3) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `name`(`name`) USING BTREE,
  UNIQUE INDEX `code`(`code`) USING BTREE,
  INDEX `idx_project_group_deleted_at`(`deleted_at`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 6 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of project_group
-- ----------------------------
INSERT INTO `project_group` VALUES (1, '运维', 'cmdb.sa', 1, '2022-10-10 00:56:18.724', '2022-10-10 00:56:18.724', NULL);
INSERT INTO `project_group` VALUES (3, 'test5项目管理员', 'test5.sa', 7, '2022-10-10 22:43:21.697', '2022-10-10 22:43:21.697', NULL);
INSERT INTO `project_group` VALUES (4, 'test6项目管理员', 'test6.sa', 8, '2022-10-10 23:13:49.796', '2022-10-10 23:13:49.796', NULL);
INSERT INTO `project_group` VALUES (5, 'test6test', 'test6.test', 8, '2022-10-10 23:13:49.796', '2022-10-10 23:13:49.796', NULL);

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking