sql - Presto中包含 ' '字符的 key 的JSON_EXTRACT问题

标签 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"
}

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

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

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

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

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

最佳答案

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 Function Changes

The :func:json_extract and :func:json_extract_scalar functions now support the square bracket syntax:

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

As part of this change, the set of characters allowed in a non-bracketed path segment has been restricted to alphanumeric, underscores and colons. Additionally, colons cannot be used in a un-quoted bracketed path segment. Use the new bracket syntax with quotes to match elements that contain special characters.

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

关于sql - Presto中包含 ' '字符的 key 的JSON_EXTRACT问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43271898/

相关文章:

java - PL/SQL 返回 CLOB 和 JDBC 性能

MySQL - 在某些行上选择 AVG,在所有行上选择 SUM

mysql - 当使用 mySQL 和 PrestoDB 的时间戳是 big int 时,有没有办法在 AWS Athena 中拉取数据范围?

window-functions - (Presto) 窗口函数 "ROWS BETWEEN"子句中 "RANGE BETWEEN"和 "OVER"的区别

mysql - 如果我只知道用户名,如何使用 SQL 语句从网站获取数据?

MySQL-在两个 group_by 之后尝试两列的 sum()

mysql 搜索逗号列表的数字范围

presto - Presto 中的用户定义函数

sql - 可以为连接中不匹配的列设置默认值吗?

hive - 如何使用带有 presto 的文本文件创建外部表?