【问题标题】:From table to JSON with Power BI / Power Query使用 Power BI / Power Query 从表到 JSON
【发布时间】:2020-11-03 17:24:09
【问题描述】:

我需要转换下表:

转成JSON格式,比如:

{
  "Inputs": {
    "input1": {
      "ColumnNames": [
        "age",
        "workclass",
        "fnlwgt",
        "education",
        "education-num",
        "marital-status",
        "occupation",
        "relationship",
        "race",
        "sex",
        "capital-gain",
        "capital-loss",
        "hours-per-week",
        "native-country"
      ],
      "Values": [
        [
          "0",
          "value",
          "0",
          "value",
          "0",
          "value",
          "value",
          "value",
          "value",
          "value",
          "0",
          "0",
          "0",
          "value"
        ],
        [
          "0",
          "value",
          "0",
          "value",
          "0",
          "value",
          "value",
          "value",
          "value",
          "value",
          "0",
          "0",
          "0",
          "value"
        ]
      ]
    }
  },
  "GlobalParameters": {}
}

这应该用作对 Web 服务的 POST 请求的主体。

所以,我尝试将以下函数应用于上表:

(InputData) =>
let
    JsonOutput = Json.FromValue(InputData),
    OutputText = Text.FromBinary(JsonOutput)
in
    OutputText

这是完整的代码:

let
    Origem = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("HY2xDsIwDET/JTOWGtoSMcIOC0IMVQcrMdSS00huVIm/x2G4e3e64abJ9Wd3cI+KleBTdsshjP5kvGJcSIpuln1vdqedFDKqMiXrl5QhCilHlDaXCrzCGzPL1/pr4UrGG0rD0YfB0JmGZs/V5gT/583N8w8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [age = _t, workclass = _t, fnlwgt = _t, education = _t, #"education-num" = _t, #"marital-status" = _t, occupation = _t, relationship = _t, race = _t, sex = _t, #"capital-gain" = _t, #"capital-loss" = _t, #"hours-per-week" = _t, #"native-country" = _t]),
    #"Tipo Alterado" = Table.TransformColumnTypes(Origem,{{"age", Int64.Type}, {"workclass", type text}, {"fnlwgt", Int64.Type}, {"education", type text}, {"education-num", Int64.Type}, {"marital-status", type text}, {"occupation", type text}, {"relationship", type text}, {"race", type text}, {"sex", type text}, {"capital-gain", Int64.Type}, {"capital-loss", Int64.Type}, {"hours-per-week", Int64.Type}, {"native-country", type text}}),
    Output = GetJson(#"Tipo Alterado")
in
    Output

但这又回来了:

[{"age":39,"workclass":"State-gov","fnlwgt":77516,"education":"Bachelors","education-num":13,"marital-status":"Never-married","occupation":"Adm-clerical","relationship":"Not-in-family","race":"White","sex":"Male","capital-gain":2174,"capital-loss":0,"hours-per-week":40,"native-country":"United-States"}]

【问题讨论】:

    标签: json powerbi powerquery


    【解决方案1】:

    根据您描述的转换,特别是使用以下 2 个函数可能有意义:

    • Table.ColumnNames
    • Table.ToRows

    小例子如下:

    let
        source = Table.FromRows(
            Json.Document(
                Binary.Decompress(
                    Binary.FromText(
                        "HY2xDsIwDET/JTOWGtoSMcIOC0IMVQcrMdSS00huVIm/x2G4e3e64abJ9Wd3cI+KleBTdsshjP5kvGJcSIpuln1vdqedFDKqMiXrl5QhCilHlDaXCrzCGzPL1/pr4UrGG0rD0YfB0JmGZs/V5gT/583N8w8=",
                        BinaryEncoding.Base64
                    ),
                    Compression.Deflate
                )
            ),
            let 
                _t = ((type nullable text) meta [Serialized.Text = true])
            in 
                type table [age = _t, workclass = _t, fnlwgt = _t, education = _t, #"education-num" = _t, #"marital-status" = _t, occupation = _t, relationship = _t, race = _t, sex = _t, #"capital-gain" = _t, #"capital-loss" = _t, #"hours-per-week" = _t, #"native-country" = _t]
        ),
    
        CreateJsonPayload = (someTable as table) as binary => Json.FromValue([
            Inputs = [
                input1 = [
                    ColumnNames = Table.ColumnNames(someTable),
                    Values = Table.ToRows(someTable)
                ]
            ],
            GlobalParameters = []
        ]),
        // If you're doing the POST request via Web.Contents, think you can pass the return value of Json.FromValue
        // directly in as the Content field value.
        // This means you wouldn't need to do Text.FromBinary (at least not for the POST request).
        payload = CreateJsonPayload(source),
        // Below is just for debugging and sanity checking purposes.
        preview = Text.FromBinary(payload)
    in
        preview
    

    注意事项:

    1. 您的示例显示数字列(如age)被编码为字符串(即"0")。如果您希望收件人将它们解码为数字,您可以调用Table.TransformColumnsTable.TransformColumnTypes 来更改类型(视情况而定),然后将转换后的表格传递给CreateJsonPayload
    2. CreateJsonPayload 函数接受任何表并返回表示 JSON 的二进制值(实际上只是字节)。该函数只是一个示例(基于您在问题中提到的预期输出)。您显然可以重构该函数,使其成为适合您的更好、更通用的解决方案。

    给我以下,我认为这符合您的预期输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多