2015-07-22 55 views
1

我們在mysql 5.6中使用這個common_schema庫從json數組中提取值。格式如下。但它返回NULL值。所以,你能幫我們解決如何使用common_schema解析json數組。使用common_schema庫無法解析json數組值

select common_schema.extract_json_value('"batter": 
[ 
    { "id": "1001", "type": "Regular" }, 
    { "id": "1002", "type": "Chocolate" }, 
    { "id": "1003", "type": "Blueberry" }, 
    { "id": "1004", "type": "Devils Food" } 
]','/id'); 

預期的輸出應保存在表作爲

id type 
    1001 Regular 
    1002 Chocolate 
    1003 Blueberry 
    1004 Devils Food 

請讓我們知道我們如何才能做到這一點的解析。

感謝 格利揚

+0

請確保您使用extract_json_value一個有效的JSON因爲這是不是一個有效的。 –

+0

您的JSON無效。您可能需要在整個字符串周圍添加大括號。 –

回答

0

直接好像不是那麼容易得到你所需要的。

一個選項,以獲得單個值:

SET @`json` := ' 
{ 
    "batter": 
     [ 
      { "id": "1001", "type": "Regular" }, 
      { "id": "1002", "type": "Chocolate" }, 
      { "id": "1003", "type": "Blueberry" }, 
      { "id": "1004", "type": "Devils Food" } 
     ] 
} 
'; 

SELECT 
    `common_schema`.`extract_json_value`(@`json`,'descendant-or-self::id[1]') `id`, 
    `common_schema`.`extract_json_value`(@`json`,'descendant-or-self::type[1]') `type`; 

+------+---------+ 
| id | type | 
+------+---------+ 
| 1001 | Regular | 
+------+---------+ 
1 row in set (0,04 sec)