【问题标题】:JSONPath: Extract single dict with keys and valuesJSONPath:使用键和值提取单个字典
【发布时间】:2017-04-23 23:30:00
【问题描述】:

我有一个在 Azure Data Lake 环境中运行的 U-SQL 应用程序。它应该处理一个充满 JSON 数据的文件,看起来像这样,除了在现实生活中超过两行。

[
{"reports" : {"direction": "FWD", "drive": "STOPS", "frob_variable": 0}},
{"reports" : {"direction": "FWD", "drive": "CRANKS", "frob_variable": -3}}
]

在那个 Data Lake 工作中,我有以下一行:

@json =
EXTRACT direction string, drive string, frob_variable int FROM @"/input/file.json"
USING new Microsoft.Analytics.Samples.Formats.Json.JsonExtractor("reports");

当我将 @json 变量的内容转储到文本文件时,我得到空值:零长度字符串和零值整数。我确实得到了正确数量的输出行,所以它必须遍历我的所有输入。

JsonExtractor 的源代码稍作研究表明,我指定的 JsonPath 值(“reports”)似乎正在返回带有嵌入 dict 的“reports”键。如果我尝试“reports.*”的 JsonPath 值,我确实会得到嵌入的值(例如,{ "FWD", "STOPS", 0 }),但我真的希望这些键与它们一起使用,所以SELECT direction, drive, frob_variable 会返回一些有用的东西。

长话短说,我正在寻找一种方法来从内部字典中提取键 值。因此,我想要的EXTRACT 的输出将是一个行集,其列是“direction”、“drive”和“frob_variable”,其值如源数据中所示。似乎应该有一个 JsonPath 解决方案或 U-SQL 中的简单解决方法。

【问题讨论】:

    标签: json jsonpath azure-data-lake u-sql


    【解决方案1】:
    @extract =
         EXTRACT 
             reports String
         FROM @"/input/file.json"
         USING new Microsoft.Analytics.Samples.Formats.Json.JsonExtractor();
    
    @relation =
        SELECT
         Microsoft.Analytics.Samples.Formats.Json.JsonFunctions.JsonTuple(reports)
         AS report
        FROM @extract;
    
    @fields =
        SELECT 
           report["direction"] AS direction,
           report["drive"] AS drive,
           Int32.Parse(report["frob_variable"]) AS frob
        FROM @relation;
    

    另见U-SQL - Extract data from json-array

    【讨论】:

    • 啊哈!这就是我一直在寻找的。 Json 数组提取!
    猜你喜欢
    • 1970-01-01
    • 2013-11-21
    • 2019-08-19
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多