Presto中包含''字符的键的JSON_EXTRACT问题

Aaq*_*aja 4 sql presto

我正在使用Presto(0.163)查询数据,并尝试从json提取字段。

我有一个像下面给出的json,它出现在'style_attributes'列中:

"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"
}
Run Code Online (Sandbox Code Playgroud)

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

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

查询失败,并显示以下错误-无效的JSON路径:“ $。attributes.Sleeve Length”

对于没有''(空格)的字段,查询运行良好。

我试图在Presto文档中找到分辨率,但是没有成功。

Dav*_*itz 7

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

要么

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

JSON功能变更

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

SELECT json_extract(json, '$.store[book]'); 
SELECT json_extract(json,'$.store["book name"]');
Run Code Online (Sandbox Code Playgroud)

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

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

  • 注意:有时语法是 `json_extract(json, '$["key_name"]') AS json` - /sf/answers/3135295831/ (3认同)