You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Power Query调用API报错:无法将List类型转换为Text类型

问题解决步骤

1. 修复URL的类型错误

你代码里的核心错误是Web.Contents的第一个参数被包裹成了列表,而该函数要求传入文本类型的URL,这直接导致了“无法将List转为Text”的报错。修改后的基础代码如下:

let
    Source = Json.Document(
        Web.Contents(
            "https://api-url.com/endpoint/id", 
            [Headers=[Authorization="Bearer "& GetToken()]]
        )
    )
in
    Source

2. 展开响应中的列表字段

修复URL后,若API返回的响应里包含列表类型的字段(比如文档中未详细说明的trip或total),需要针对性展开:

情况1:响应是单条记录,其中某个字段是列表

假设trip字段是列表类型,可按以下步骤展开并合并其他字段:

let
    Source = Json.Document(
        Web.Contents(
            "https://api-url.com/endpoint/id", 
            [Headers=[Authorization="Bearer "& GetToken()]]
        )
    ),
    // 将trip列表转为表格
    TripTable = Table.FromList(Source[trip], Splitter.SplitByNothing()),
    // 展开列表中包含的记录字段(替换为实际的字段名)
    ExpandedTrip = Table.ExpandRecordColumn(TripTable, "Column1", {"distance", "duration"}, {"trip_distance", "trip_duration"}),
    // 添加其他非列表字段
    AddDeviceInfo = Table.AddColumn(ExpandedTrip, "device_id", each Source[id]),
    AddServiceDate = Table.AddColumn(AddDeviceInfo, "last_serviced_date", each Source[lastServicedDate])
in
    AddServiceDate

情况2:响应本身是多条记录的列表

如果API返回的是包含多个设备记录的列表,先转成表格再展开所有字段:

let
    Source = Json.Document(
        Web.Contents(
            "https://api-url.com/endpoint/id", 
            [Headers=[Authorization="Bearer "& GetToken()]]
        )
    ),
    // 将整个响应列表转为表格
    MainTable = Table.FromList(Source, Splitter.SplitByNothing()),
    // 展开每条记录的所有字段
    ExpandedRecords = Table.ExpandRecordColumn(MainTable, "Column1", {"id", "lastServicedDate", "trip", "total", "stateLastUpdated"}, {"id", "lastServicedDate", "trip", "total", "stateLastUpdated"}),
    // 若trip是列表,继续展开该列
    ExpandedTripList = Table.ExpandListColumn(ExpandedRecords, "trip")
in
    ExpandedTripList

操作提示

  • 在Power Query编辑器中点击Source步骤的预览图标,查看实际返回的JSON结构,确认哪些字段是列表类型。
  • 展开时的字段名必须和API返回的字段完全一致,可从预览中复制字段名避免拼写错误。

内容的提问来源于stack exchange,提问作者AlbaTroels

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 17:15:32