2016-08-11 81 views
0

JSON/MEMSQL存在問題。這裏是我的表:從MEMSQL讀取JSON

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的第二個價值,因爲它是一個基於零的索引。

我還可以閱讀對象的身體數組:

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

返回的第一個對象數組中:

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

但是,我無法找到一個方法來讀此對象的個別屬性。

任何人有任何想法?

回答

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) 
+0

感謝您的回覆!這工作。 – BamBam