【问题标题】:Power Bi Query to web service - Error: Expression.Error: We cannot convert the value to type RecordPower Bi 查询到 Web 服务 - 错误:Expression.Error:我们无法将值转换为记录类型
【发布时间】:2023-04-03 12:41:01
【问题描述】:

我有一个生成 JSON 的 Web 服务。我们对其进行 jQuery REST 调用并将数据绑定到表。

服务是 C# WEBAPI,代码如下:

  data = serializer.Serialize(rows);
  return Request.CreateResponse(HttpStatusCode.OK, lstFilteredData, Configuration.Formatters.JsonFormatter);

它会生成这样的 JSON:

"[{\"School\":\"UM \",\"Students\":\"500\"},{\"School\":\"FIU \",\"Students\":\"700\"},{\"School\":\"UF \",\"Students\":\"600\"},{\"School\":\"GT \",\"Students\":\"300\"}]"

我们有 jQuery REST 可以像这样成功使用服务:

 $.ajax({
        url: 'https://myservices')),
        type: 'GET',
        dataType: 'json',
        cache: false,
        crossDomain: true,
        //async: false,
        success: function (data){  onQuerySucceededWeb(data,true,param);}
    }); 

我正在尝试使用 Power Bi 报告该数据。我的 PowerBi 查询脚本是: 让

Source = Json.Document(Web.Contents("https://mywebservices")),
  #"Converted to Table" = Record.ToTable(Source),
    #"Expanded Value" = Table.ExpandListColumn(#"Converted to Table", "Value"),
    #"Expanded Value1" = Table.ExpandRecordColumn(#"Expanded Value", "Value", {"School", "Students"}, {"Value.School", "Value.Students"})

in 
  #"Expanded Value1"

我收到此错误:

**Expression.Error: We cannot convert the value "[{"School":"UM      ..." to type Record.**
Details:
    Value=[{"School":"UM        ","Students":"500"},{"School":"FIU       ","Students":"700"},{"School":"UF        ","Students":"600"},{"School":"GT        ","Students":"300"}]
    Type=Type

【问题讨论】:

  • 如果你使用 = Text.FromBinary(Web.Contents("mywebservices")) 会出现什么?
  • 我应该把它放在哪里?代替我的整个查询脚本?
  • 相同。 Expression.Error:我们无法将值“”[{\“School\”:\“UM ...”转换为类型记录。
  • 如果将Record.ToTable(Source)替换为Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),然后将后面两步替换为Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"School", "Students"}, {"School", "Students"})会怎样
  • Expression.Error: 我们无法将值 ""[{\"School\":\"UM ..." 转换为类型列表。详情:

标签: json rest powerbi powerquery


【解决方案1】:

您的服务器上可能出现了问题,因为您的 JSON 是一个对象的 JSON 字符串。如果服务器没有对 JSON 文本进行字符串化,即按字面意思生成这些字节,您的 M 查询就会起作用:

[{"School":"UM ","Students":"500"},{"School":"FIU ","Students":"700"},{"School":"UF ","Students":"600"},{"School":"GT ","Students":"300"}]

如果你只是想让你的 M 再次工作,你可以对 JSON 进行双重解码:

= Json.Document(Json.Document(Web.Contents("https://mywebservices")))

【讨论】:

  • 是的,原来我们在服务中进行了双重编码。
【解决方案2】:

Json.Document 返回一个文本值,因为返回的 JSON 是一个字符串。如果删除引号,Json.Document 应将其解析为对象列表。这应该有效:

let
    Source = Web.Contents("https://mywebservices")
    Custom1 = Text.Range(Source, 1, Text.Length(Source) - 2),
    Custom2 = Json.Document(Custom1),
    #"Converted to Table" = Table.FromList(Custom2, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"School", "Students"}, {"School", "Students"})
in
    #"Expanded Column1"`

如果网站曾经返回一个空字符串,这将失败。

【讨论】:

  • 这一行:= Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"School", "Students"}, {"School", "Students"}) 产生了这个错误:Expression.Error:我们无法将二进制类型的值转换为文本类型。详细信息:值=二进制类型=类型
  • 在浏览器中查看从服务返回的实际屏幕截图。
  • 错误其实就在这一行:= Text.Range(Source, 1, Text.Length(Source) - 2)
  • 糟糕,将第一步替换为Source = Text.FromBinary(Web.Contents("https://mywebservices")),。这应该将网络请求转换为文本,然后可以将 JSON 字符串转换为 JSON 数组,该数组可以通过 Json.Document 转换为列表。
  • = Json.Document(Custom1) DataFormat.Error: 我们在 JSON 输入的末尾发现了多余的字符。详细信息:值=\位置=2
猜你喜欢
  • 1970-01-01
  • 2022-12-22
  • 1970-01-01
  • 2021-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-22
  • 2021-12-20
相关资源
最近更新 更多