【问题标题】:Reading JSON from MEMSQL从 MEMSQL 读取 JSON
【发布时间】:2016-08-11 17:36:19
【问题描述】:

JSON/MEMSQL 有问题。这是我的桌子:

CREATE TABLE `MEMSQLPOLYGLOT`  ( 
    ID BIGINT AUTO_INCREMENT,
    `DATA`  JSON NULL,
    PRIMARY KEY (ID)
);

这是我要阅读的记录:

 insert into MEMTEST (DATA) values 
('
{
    "EnterpriseMessage" : 
        {
            "Body" :
            [
                {
                    "AccountNumber":"ABCD",
                    "AdminCompany":null,
                    "BrokerNumber":"WWonka"
                },
                {
                    "AccountNumber":"CSNE",
                    "AdminCompany":null,
                    "BrokerNumber":"ZWiza"
                }
            ],
        "Header" :
            {
                "mimetye":"application/vnd.ms-powerpoint",
                "destinationsystem":"ETL",
                "requiresack":"FALSE",
                "SimpArr": 
                    [
                        "BYTS6181",
                        "EVU98124",
                        "Genesys"
                    ],
                "EmptyFile":1
            }
    }

}
');

我可以读取标题中的 SimpArr 数组。

SELECT DATA::EnterpriseMessage::Header::SimpArr from MEMTEST;

返回:

["BYTS6181","EVU98124","Genesys"]

我也可以传入一个键索引来获取特定的值,比如:

select JSON_EXTRACT_JSON(DATA::EnterpriseMessage::Body, 1) from MEMTEST;

这将返回 SimpArr 的第二个值,因为它是从零开始的索引。

我还可以读取 Body 中的对象数组:

select JSON_EXTRACT_JSON(DATA::EnterpriseMessage::Body, 0) from MEMTEST;

返回该数组中的第一个对象:

{
    "AccountNumber":"ABCD",
    "AdminCompany":null,
    "BrokerNumber":"WWonka"
}

但是,我无法找到读取此对象的各个属性的方法。

有人有什么想法吗?

【问题讨论】:

    标签: json singlestore


    【解决方案1】:

    您可以将密钥作为额外参数传递给JSON_EXTRACT_JSON

    mysql> select JSON_EXTRACT_JSON(DATA::EnterpriseMessage::Body, 0, "AccountNumber") from MEMTEST;
    +----------------------------------------------------------------------+
    | JSON_EXTRACT_JSON(DATA::EnterpriseMessage::Body, 0, "AccountNumber") |
    +----------------------------------------------------------------------+
    | "ABCD"                                                               |
    +----------------------------------------------------------------------+
    1 row in set (0.08 sec)
    

    JSON_EXTRACT_JSON 接受任意数量的参数,字符串键或整数数组索引。例如,我们可以在上面的示例中展开 :: 语法:

    select JSON_EXTRACT_JSON(DATA, "EnterpriseMessage", "Body", 0, "AccountNumber") from MEMTEST;
    

    还要注意返回值中有双引号。那是因为您提取 JSON,并且字符串的 JSON 表示带有双引号。如果你真的想得到字符串,使用JSON_EXTRACT_STRING:

    mysql> select JSON_EXTRACT_STRING(DATA, "EnterpriseMessage", "Body", 0, "AccountNumber") from MEMTEST;
    +----------------------------------------------------------------------------+
    | JSON_EXTRACT_STRING(DATA, "EnterpriseMessage", "Body", 0, "AccountNumber") |
    +----------------------------------------------------------------------------+
    | ABCD                                                                       |
    +----------------------------------------------------------------------------+
    1 row in set (0.07 sec)
    

    【讨论】:

      猜你喜欢
      • 2017-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-12
      • 2012-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多