3

I'm trying to return the results of a poll in JSON format. My first SQL is this

SELECT
fa.pollQuestion_id, 
q.title, 
JSON_OBJECT(
    'text', qo.title, 
    'total', COUNT(fao.pollQuestionOption_id)
) values
FROM pollFillAnswers fa 
INNER JOIN pollFills f ON fa.pollFill_id = f.pollFill_id
INNER JOIN pollQuestions q ON fa.pollQuestion_id = q.pollQuestion_id
INNER JOIN pollFillAnswerOptions fao ON fao.pollFillAnswer_id = 
fa.pollFillAnswer_id 
WHERE f.poll_id = 17 AND fa.type IN (3,4,5) 
GROUP BY fao.pollQuestionOption_id;

This SQL return something like that:

pollQuestion_id title values

163 Question 1 {"text": "No", "total": 599}

163 Question 1 {"text": "Si", "total": 258}

In my second SQL I use GROUP BY pollQuestion_id and GROUP_CONCAT(values) to aggregate the results with this SQL

SELECT JSON_OBJECT(
        'title', result.title, 
        'values', GROUP_CONCAT(values)
    ) answers 
    FROM (
        SELECT
            q.title,
            JSON_OBJECT(
                'text', qo.title, 
                'total', COUNT(fao.pollQuestionOption_id)
            ) values
        FROM pollFillAnswers fa 
        INNER JOIN pollFills f ON fa.pollFill_id = f.pollFill_id
        INNER JOIN pollQuestions q ON fa.pollQuestion_id = q.pollQuestion_id
        INNER JOIN pollFillAnswerOptions fao ON fao.pollFillAnswer_id = fa.pollFillAnswer_id 
        WHERE f.poll_id = 17 AND fa.type IN (3,4,5) 
        GROUP BY fao.pollQuestionOption_id
    ) AS result 
    GROUP BY result.pollQuestion_id;

The result is as follows

163 {"title": "Question 1", "values": "{\"text\": \"No\", \"total\": 599},{\"text\": \"Si\", \"total\": 258}"}

group_contact() add slashes in all of the strings within the JSON, why? I try to remove the slashes with REPLACE()or JSON_UNQUOTES()but I can`t eliminate this behavior.

monchyrcg
  • 372
  • 1
  • 6
  • 16

1 Answers1

0

Please try the below code once. and if you don't need , replace this with ''.

GROUP_CONCAT(REPLACE(values, ',', '\\,')) 
Lavish Tyagi
  • 223
  • 5
  • 10