![](/img/trans.png)
[英]Query for Power BI Output from Azure Stream Analytics with JSON data
[英]From table to JSON with Power BI / Power Query
我需要轉換下表:
轉成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"}]
根據您描述的轉換,特別使用以下 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
一些注意事項:
age
)被編碼為字符串(即"0"
)。 如果您希望收件人將它們解碼為數字,您可以調用Table.TransformColumns
或Table.TransformColumnTypes
來更改類型(視情況而定),然后將轉換后的表傳遞給CreateJsonPayload
。CreateJsonPayload
函數接受任何表並返回表示 JSON 的二進制值(實際上只是字節)。 該函數只是一個示例(基於您在問題中提到的預期輸出)。 您顯然可以重構該函數,使其成為適合您的更好的、通用的解決方案。給我以下內容,我認為這與您的預期輸出相符:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.