【问题标题】:Add nested object to json data in Mysql databse将嵌套对象添加到 Mysql 数据库中的 json 数据中
【发布时间】:2020-11-15 09:27:35
【问题描述】:

我尝试了很多方法来将嵌套对象添加到我的 mysql 列中,但它只是将它添加为新键。这是我的示例:

{
    "Vocab": {
        "username":"132423424",

        "Mother": {
           "code":"1",
      "progress":"1",
      "nextdate":"22-10-2020",
 "lastcheck":"22-10-2020"
        },
        "Father": {
           "code":"2",
      "progress":"2",
      "nextdate":"22-10-2020",
       "lastcheck":"22-10-2020"
        } 
    }
}

我想在 Vocab 对象下添加该对象及其内部键:

   "Brother": {
           "code":"3",
      "progress":"1",
      "nextdate":"22-12-2020",
       "lastcheck":"22-12-2020"
        } 

任何帮助将不胜感激

【问题讨论】:

    标签: mysql json nested-object


    【解决方案1】:

    一种选择是使用JSON_INSERT() 函数:

    SET @data = '{
        "Vocab": {
            "username":"132423424",
            "Mother": {
            "code":"1",
            "progress":"1",
            "nextdate":"22-10-2020",
            "lastcheck":"22-10-2020"
            },
            "Father": {
            "code":"2",
            "progress":"2",
            "nextdate":"22-10-2020",
            "lastcheck":"22-10-2020"
            } 
        }
    }';
    
    SET @data2 = '{ "code": "3", "progress":"1", "nextdate":"22-12-2020", "lastcheck":"22-12-2020" }';
    
    
    SELECT JSON_PRETTY(
              REPLACE( 
                     REPLACE(
                             REPLACE(
                                     JSON_INSERT(@data, '$.Vocab.Brother', (@data2))
                                    ,'\\"','"')
                             ,'"{','{') 
                    ,'}"','}') ) AS 'New JSON';
    

    Demo

    最后需要使用REPLACE() 函数来格式化与双引号相关的格式。

    【讨论】:

      【解决方案2】:

      当您使第二个数据也是有效的 JSON 时 喜欢

       {   "Brother": {
             "code":"3",
        "progress":"1",
        "nextdate":"22-12-2020",
         "lastcheck":"22-12-2020"
          } }
      

      你使用JSON_MERGE_PATCH

      SET @a = '{
          "Vocab": {
              "username":"132423424",
      
              "Mother": {
                 "code":"1",
            "progress":"1",
            "nextdate":"22-10-2020",
       "lastcheck":"22-10-2020"
              },
              "Father": {
                 "code":"2",
            "progress":"2",
            "nextdate":"22-10-2020",
             "lastcheck":"22-10-2020"
              } 
          }
      }';
      SET @b = '{   "Brother": {
                 "code":"3",
            "progress":"1",
            "nextdate":"22-12-2020",
             "lastcheck":"22-12-2020"
              } }';
      
      ✓ ✓
      SELECT JSON_MERGE_PATCH(@a, @b);
      
      | JSON_MERGE_PATCH(@a, @b) | | :------------------------------------------------ -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ---------------------- | | {“词汇”:{“父亲”:{“代码”:“2”,“下一个日期”:“22-10-2020”,“进度”:“2”,“lastcheck”:“22-10-2020” },“母亲”:{“代码”:“1”,“下一个日期”:“22-10-2020”,“进度”:“1”,“lastcheck”:“22-10-2020”},“用户名": "132423424"}, "兄弟": {"code": "3", "nextdate": "22-12-2020", "progress": "1", "lastcheck": "22-12-2020" "}} |

      db小提琴here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-10
        • 2016-12-11
        • 1970-01-01
        相关资源
        最近更新 更多