powerquery - How do I add quotation marks to the beginning and end of a string or variable in Power Query M?
问题描述
This is the code I have:
let
content = "{
""timezone"": ""tokyo standard time""
}",
Source = Json.Document(Web.Contents("localhost:7071/api/Function1", [Content=Text.ToBinary(content)]))
in
Source
It works great. I want to be able to enter whichever timezone I want on the fly, however. I've tried something like this:
(Timezone) =>
let
data = Text.Combine({"timezone", Timezone, ":"}),
content = "{
"&data&"
}",
Source = Json.Document(Web.Contents("localhost:7071/api/Function1", [Content=Text.ToBinary(content)]))
in
Source
This almost works, but I need to add quotation marks around the "Timezone" input parameter/variable for it to be valid JSON.
I've tried using the Text.PadStart() and Text.PadEnd() functions but I can't get them to add the " character.
解决方案
You've already found a solution yourself, but just thought I'd add that it might be better (in terms of readability and maintainability) to use Json.FromValue
(details: https://docs.microsoft.com/en-us/powerquery-m/json-fromvalue).
Saves you having to imperatively build the JSON by hand and escaping/handling double quotes correctly. Example implementation in your case might be something like:
(Timezone) =>
let
body = Json.FromValue([timezone = Timezone]),
Source = Json.Document(Web.Contents("localhost:7071/api/Function1", [Content = body]))
in
Source
Since Json.FromValue
returns a value of type binary
, you can pass it directly as Content
field value (without needing to invoke Text.ToBinary
).
推荐阅读
- sql - 如何根据年和周列排除周?
- unity3d - 在我的 C# 代码中使用 Excel 中的大数据的最有效方法是什么?
- python - 如何从 csv 读取第一列并分成多维数组
- python - xarray - 使用 groupby 对一年中的每一天的气候每小时 netCDF 数据进行分组
- node.js - 猫鼬突然坏了,没有得到回调
- loops - 将整数值相加并在不同的类中使用它们
- html - 如何影响 HTML5 进度元素的最小宽度?
- azure - 具有恢复服务保管库位置的 Azure 存储帐户位置
- sql - 如何使用 case 语句使用计数
- asp.net-core - 无法将 ASP.NET Core 的 IdentityServer4 代码示例托管为 Windows 服务