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

Issues with JSON_EXTRACT in Presto for keys containing ' ' character

我正在使用 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"
}

我无法提取字段 'Short Sleeves'。 以下是我正在使用的查询:

Select JSON_EXTRACT(style_attributes,'$.attributes.Sleeve Length') as length from table;

查询失败并出现以下错误 - 无效 JSON 路径:'$.attributes.Sleeve 长度'

对于没有“ ”的字段 (space),查询 运行 没问题。

我试图在 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

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


JSON : {"Travel Date":"2017-9-22", "City": "Seattle"}
栏目名称:ITINERARY 我想从当前 JSON 中提取 'Travel Date' 然后:

查询:SELECT JSON_EXTRACT(ITINERARY, "$.\"Travel Date\"") from Table

注意:只需在键名的开头和结尾添加\"

希望这一定能满足您的需要。 :)

SELECT 
   tags -- It is column with Json string data
  ,json_extract(tags , '$.Brand') AS Brand
  ,json_extract(tags , '$.Portfolio') AS Portfolio
  ,cost
FROM
    TableName

Sample data for tags - {"Name": "pxyblob",  "Owner": "",  "Env": "prod",  "Service": "",  "Product": "",  "Portfolio": "OPSXYZ",  "Brand": "Limo",  "AssetProtectionLevel": "",  "ComponentInfo": ""}