Reading JSON from MEMSQL

懵懂的女人 提交于 2019-12-11 04:17:21

问题


Having a problem with JSON / MEMSQL. Here's my table:

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

Here's the record I'm trying to read:

 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
            }
    }

}
');

I can read the SimpArr array in the header.

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

Returns:

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

I can also pass in a key index to get a specific value, such as:

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

This returns the 2nd value of the SimpArr since it's a zero-based index.

I can also read the array of objects in the Body:

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

which returns the first object in that array:

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

However, I am unable to find a way to read the individual attributes of this object.

Anyone have any ideas?


回答1:


You can just pass the key as an extra argument to 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 takes any number of arguments, either string keys or integer array indices. For example, we can unroll the :: syntax in your example above:

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

Also note that the return value has double quotes in it. That's because you extract JSON, and the JSON representation of a string has double quotes. If you actually want to get the string, use 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)


来源:https://stackoverflow.com/questions/38902777/reading-json-from-memsql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!