1

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

Juliano Costa
  • 437
  • 6
  • 10
  • 1
    Let us see the code please, a letter is nice but you cannot beat showing us the code – RiggsFolly Jan 30 '20 at 16:41
  • Possible you can find the answer here: https://stackoverflow.com/questions/41758870/how-to-convert-result-table-to-json-array-in-mysql – Slava Rozhnev Jan 30 '20 at 18:00
  • Does this answer your question? [How to convert result table to JSON array in MySQL](https://stackoverflow.com/questions/41758870/how-to-convert-result-table-to-json-array-in-mysql) – nbk Jan 30 '20 at 18:17
  • I got it now. I used JSON_ARRAYAGG with JSON_OBJECT, both together. – Juliano Costa Jul 12 '20 at 22:40
  • Do all steps together with only one select, as below: 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) ) INTO json FROM v_case AS v; – Juliano Costa Jul 12 '20 at 23:05

3 Answers3

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
Juliano Costa
  • 437
  • 6
  • 10
0
    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;
Juliano Costa
  • 437
  • 6
  • 10
0

This is not an array, you have to remove {}

{[
        {
            "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."
        }
]}
Chris Catignani
  • 5,040
  • 16
  • 42
  • 49