Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-22837

JSON_ARRAYAGG and JSON_OBJECTAGG treat JSON arguments as text

Details

    • Bug
    • Status: Closed (View Workflow)
    • Major
    • Resolution: Fixed
    • 10.5
    • 10.5.4
    • JSON
    • None

    Description

      I'm looking at how JSON_ARRAYAGG and JSON_OBJECTAGG work at processing JSON
      documents.

      I'm trying these examples:

      create table t30 (col1 json);
      insert into t30 values('{"color":"red", "size":1}' );
      insert into t30 values('{"color":"blue", "size":2}' );
      

      MariaDB [test]> select JSON_ARRAYAGG(col1) from t30;
      +------------------------------------------------------------------------+
      | JSON_ARRAYAGG(col1)                                                    |
      +------------------------------------------------------------------------+
      | ["{\"color\":\"red\", \"size\":1}","{\"color\":\"blue\", \"size\":2}"] |
      +------------------------------------------------------------------------+
      

      while on MySQL-8, one gets:

      select JSON_ARRAYAGG(col1) from t30;
      +-------------------------------------------------------------+
      | JSON_ARRAYAGG(col1)                                         |
      +-------------------------------------------------------------+
      | [{"size": 1, "color": "red"}, {"size": 2, "color": "blue"}] |
      +-------------------------------------------------------------+
      

      The same with JSON_OBJECTAGG:

      create table t31 (name varchar(100), contents json);
      insert into t31 values('obj1', '{"color":"blue", "size":2}' );
      insert into t31 values('obj2', '{"color":"red", "size":1}' );
      

      MariaDB [test]> select JSON_OBJECTAGG(name, contents) from t31;
      +---------------------------------------------------------------------------------------+
      | JSON_OBJECTAGG(name, contents)                                                        |
      +---------------------------------------------------------------------------------------+
      | {"obj1":"{\"color\":\"blue\", \"size\":2}", "obj2":"{\"color\":\"red\", \"size\":1}"} |
      +---------------------------------------------------------------------------------------+
      

      While on MySQL-8 it is:

      select JSON_OBJECTAGG(name, contents) from t31;
      +-----------------------------------------------------------------------------+
      | JSON_OBJECTAGG(name, contents)                                              |
      +-----------------------------------------------------------------------------+
      | {"obj1": {"size": 2, "color": "blue"}, "obj2": {"size": 1, "color": "red"}} |
      +-----------------------------------------------------------------------------+
      

      Attachments

        Issue Links

          Activity

            psergei Sergei Petrunia added a comment - - edited

            According to holyfoot:

            There is a way to specify explicitly the argument is JSON, so kind of workaround:

            MariaDB [test]> select JSON_ARRAYAGG(json_compact(col1)) from t30;
            +------------------------------------------------------+
            | JSON_ARRAYAGG(json_compact(col1))                    |
            +------------------------------------------------------+
            | [{"color":"red","size":1},{"color":"blue","size":2}] |
            +------------------------------------------------------+
            1 row in set (0.00 sec)
            

            but it should work with JSON columns, too.

            psergei Sergei Petrunia added a comment - - edited According to holyfoot : There is a way to specify explicitly the argument is JSON, so kind of workaround: MariaDB [test]> select JSON_ARRAYAGG(json_compact(col1)) from t30; +------------------------------------------------------+ | JSON_ARRAYAGG(json_compact(col1)) | +------------------------------------------------------+ | [{"color":"red","size":1},{"color":"blue","size":2}] | +------------------------------------------------------+ 1 row in set (0.00 sec) but it should work with JSON columns, too.
            holyfoot Alexey Botchkov added a comment - https://github.com/MariaDB/server/commit/e290e5a75d0c2b12133c2ed118e3bc9bd26a266d

            People

              holyfoot Alexey Botchkov
              psergei Sergei Petrunia
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Git Integration

                  Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.