创建一个导致嵌套 JSON 数组对象的 MySQL 过程

Posted

技术标签:

【中文标题】创建一个导致嵌套 JSON 数组对象的 MySQL 过程【英文标题】:Create a MySQL procedure that result in a nested JSON Array Object 【发布时间】:2021-12-30 14:45:43 【问题描述】:

我需要在 mysql 中创建一个存储过程,从数据库中获取一些字段并创建一个嵌套的 json 对象:

首先我创建一个 json 对象,如下所示:


    "form": "Exams tests",
    "version": "v3.001.01",
    "questions": []

其次是这样的 json 数组对象:

[
        
            "ordem": 1,
            "num_questions": 1,
            "question1": "How old are you?"
            "answer1": "I'm 18 years old."
        
        
            "ordem": 2,
            "num_questions": 2,
            "question1": "How old are you?"
            "answer1": "I'm 18 years old."
            "question2": "Where do you live?"
            "answer2": "I live in Boston."
        
        
            "ordem": 3,
            "num_questions": 1,
            "question1": "How old are you?"
            "answer1": "I'm 23 years old."
        
]

结果查询显示如下:


    "form": "Exams tests",
    "version": "v3.001.01",
    "questions": [
        
            "ordem": 1,
            "num_questions": 1,
            "question1": "How old are you?"
            "answer1": "I'm 18 years old."
        
        
            "ordem": 2,
            "num_questions": 2,
            "question1": "How old are you?"
            "answer1": "I'm 18 years old."
            "question2": "Where do you live?"
            "answer2": "I live in Boston."
        
        
            "ordem": 3,
            "num_questions": 1,
            "question1": "How old are you?"
            "answer1": "I'm 23 years old."
        
    ]

我在尝试将嵌套的 json 数组插入 json 对象时出错

【问题讨论】:

请让我们看一下代码,一封信很好,但您不能比向我们展示代码更好 可能你可以在这里找到答案:***.com/questions/41758870/… 这能回答你的问题吗? How to convert result table to JSON array in MySQL 我现在明白了。我将 JSON_ARRAYAGG 与 JSON_OBJECT 一起使用。 只用一个 select 完成所有步骤,如下所示: SELECT JSON_OBJECT( 'form', v.form_name, 'version', v.version, 'questions, ( select json_arrayagg(json_object( 'ordem ',tb_questions.order, 'num_questions',tb_questions.num 'question1',tb_questions.question1'answer1',tb_questions.answer1 )) 来自 tb_questions json FROM v_case AS v; 【参考方案1】:

我加入 JSON_OBJECTjson_arrayagg 如下:


    "name": "Moon",
    "resume": "This is a resume.",
    "dt_ini": "2018-09-01",
    "dt_end": null,
    "cases": [
        
            "unit": "unit 1",
            "unit_id": 10
        ,
        
            "unit": "unit 2",
            "unit_id": 290
        ,
        
            "unit": "unit 3",
            "unit_id": 44
        ,
        
            "unit": "unit 4",
            "unit_id": 108
        
    ]

最终结果是这样的:

CREATE DEFINER=`root`@`localhost` PROCEDURE `get_list_case`(
IN `code` int,
IN `base_code` int)
BEGIN

    DECLARE json TEXT DEFAULT '';

    SELECT JSON_OBJECT(
        'name', v.name, 
        'dt_ini', v.dt_ini, 
        'dt_end', v.dt_end, 
        'resumo', v.resumo,
        'cases', ( select json_arrayagg(json_object(
                                'unit_id',`tb_unit`.`unit_id`,
                                'unit',`tb_unit`.`unit`))
                            from tb_unit_case
                                INNER JOIN tb_unit ON tb_unit_case.unid_code = tb_unit.unit_id
                            WHERE tb_unit_case.case_code = code)
    ) INTO json
    FROM v_case AS v
    WHERE v.code = code and v.base_code = base_code;
    
    SELECT json;
    
END

【讨论】:

【参考方案2】:
    DECLARE json TEXT DEFAULT '';
    DECLARE code INTEGER;

    SET code = 1;

    SELECT JSON_OBJECT(
        'form', v.form_name, 
        'version', v.version, 
        'questions, ( select json_arrayagg(json_object(
                                'ordem',`tb_questions`.`order`,
                                'num_questions',`tb_questions`.`num`
                                'question1',`tb_questions`.`question1`
                                'answer1',`tb_questions`.`answer1`
                            ))
                            from tb_questions
                            WHERE tb_questions.code = code)
    ) INTO json
    FROM v_case AS v
    WHERE v.code = code;

【讨论】:

以上是关于创建一个导致嵌套 JSON 数组对象的 MySQL 过程的主要内容,如果未能解决你的问题,请参考以下文章

JSON 返回嵌套数组而不是对象 [Spring boot + JPA + MySQL + REST]

C# 使用字符串数组中的嵌套对象动态创建 JSON

将 Json 数组嵌套到对象

使用 php mysql 创建嵌套的 json 对象

使用嵌套映射的 JSON 对象反序列化,Java Spring

React:将嵌套的 json 数据与父数据合并以创建多个对象