2017-04-07 1004 views
2

我正在使用Presto(0.163)来查询数据,并试图从json中提取字段。JSON_EXTRACT在Presto中包含''字符的问题

我有这样一个下面给出,这是存在于列“style_attributes”一个JSON:

"attributes": { 
    "Brand Fit Name": "Regular Fit", 
    "Fabric": "Cotton", 
    "Fit": "Regular", 
    "Neck or Collar": "Round Neck", 
    "Occasion": "Casual", 
    "Pattern": "Striped", 
    "Sleeve Length": "Short Sleeves", 
    "Tshirt Type": "T-shirt" 
} 

我无法提取字段“短袖”。 以下是我正在使用的查询:

从表中选择JSON_EXTRACT(style_attributes,'$。属性.Sleeve长度')作为长度;

查询失败,出现以下错误 - 无效的JSON路径:“$ .attributes.Sleeve长度”

对于字段,而'“(空间),查询运行良好。

我试图在Presto文档中找到解决方案,但没有成功。

回答

3
presto:default> select json_extract_scalar('{"attributes":{"Sleeve Length": "Short Sleeves"}}','$.attributes["Sleeve Length"]'); 
    _col0 
--------------- 
Short Sleeves 

presto:default> select json_extract_scalar('{"attributes":{"Sleeve Length": "Short Sleeves"}}','$["attributes"]["Sleeve Length"]'); 
    _col0 
--------------- 
Short Sleeves 

JSON功能变化

的:函数:json_extract和:FUNC:json_extract_scalar函数现在 支持方括号语法:

SELECT json_extract(json, '$.store[book]'); 
SELECT json_extract(json,'$.store["book name"]'); 

作为此更改的一部分,允许在非括号路径段中的字符集 限制为 字母数字,下划线和冒号。此外,冒号不能用于未加引号的括号中的路径段中的 。使用带括号的新括号语法 来匹配包含特殊字符的元素。

https://github.com/prestodb/presto/blob/c73359fe2173e01140b7d5f102b286e81c1ae4a8/presto-docs/src/main/sphinx/release/release-0.75.rst

+0

这有帮助。谢谢。 –

+0

不客气。附:不要忘记接受答案 –

0

这是你的正确答案。 让你说:


JSON:{ “旅游日”: “2017年9月22日”, “城市”: “西雅图”}
列名:线路 而我瓦纳提取物 '旅游日期' “从表

注:只需添加\ SELECT JSON_EXTRACT(行程, ”$ \。“ 旅行日期\”):”形成当前JSON则:

查询的起始和结束键名

希望这肯定会适合您的需要。 :)