3

I've try return json object using "concat" and "group_concat" functions. Problem is that I need to use group_concat but I want to have a valid JSON structure. What I do wrong?

  1. {"a":"a", "b": "b", "id": null}
  2. [{"id": "123"}]

...

SELECT JSON_REPLACE((
                  SELECT JSON_OBJECT(
                             'a', 'a',
                             'b', 'b',
                             'id', null
                           )), '$.id', (
                  SELECT CONCAT(
                             '[', group_concat(JSON_OBJECT(
                          'id',
                          '123')),
                             ']'))
     )

result: {"a": "a", "b": "b", "id": "[{\"id\": \"123\"}]"}

expected: {"a": "a", "b": "b", "id": [{"id": "123"}]}

2 Answers 2

5

Extra CASTing AS JSON for the third argument of JSON_REPLACE fixes the issue :

SELECT JSON_REPLACE((
                  SELECT JSON_OBJECT(
                             'a', 'a',
                             'b', 'b',
                             'id', null
                           )), '$.id', 
                         CAST( CONCAT('[', GROUP_CONCAT(
                                            JSON_OBJECT('id', '123')),
                                      ']') AS JSON )) as "Result JSON"
Result :
{
  "a": "a",
  "b": "b",
  "id": [
    {
      "id": "123"
    }
  ]
}

Demo

Sign up to request clarification or add additional context in comments.

3 Comments

but did you examine the resulting syntax after and before applying that ? Please consider the Demo
You are right; Due to group_concat, it is getting converted to string and thus this double quote issue. +1
It was very helpful. I spent a lot of hours tried to resolve this issue. Thanks for involvement.
2

this is what you need JSON_ARRAYAGG

  SELECT JSON_REPLACE((SELECT JSON_OBJECT(
                             'a', 'a',
                             'b', 'b',
                             'id', null
                           )), '$.id', 
            (SELECT JSON_ARRAYAGG(JSON_OBJECT('id','123')))           
  )

if not using JSON_ARRAYAGG

  SELECT JSON_REPLACE((SELECT JSON_OBJECT(
                             'a', 'a',
                             'b', 'b',
                             'id', null
                           ))
            , '$.id'
            ,  (SELECT JSON_ARRAY(CAST(group_concat(JSON_OBJECT('id','123')) as json)))
   )

7 Comments

I forget to mention that is not possible I use Amazon aurora MySQL and my version doesn't have this function
yes. JSON_ARRAY is available. if I change without using "group_concat" function I get "subquery return more than 1 row".
you don't need subquery, remove select on the 2nd column
Is the main issue. In production code I have subquery and I need unjumble this JSON together. And the second subquery will return couple rows.
It was very helpful. I spent a lot of hours tried to resolve this issue. Thanks for involvement.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.