Create a MySQL procedure that result in a nested JSON Array Object

风流意气都作罢 提交于 2021-01-29 10:16:58

问题


I need to create a stored procedure in mysql that take some fields from DB and create a neasted json object:

Firstly I create a json object as showing below:

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

And secondly a json array object like this:

{[
        {
            "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."
        }
]}

And the result query showld be something like this:

{
    "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."
        }
    ]}
}

I got an error when I'm trying to insert a nested json array into a json object


回答1:


I joined JSON_OBJECT with json_arrayagg as below:

{
    "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
        }
    ]
}

The final result is this:

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;


来源:https://stackoverflow.com/questions/59990553/create-a-mysql-procedure-that-result-in-a-nested-json-array-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!