簡體   English   中英

Google Data Studio - 自定義連接器 - 獲取數據解析 json

[英]Google Data Studio - Custom Connector - Get Data parsing json

我正在嘗試在 GDS 上為外部 API 構建自定義連接器。

我已經使用來自 API 的所有字段配置了架構,當我部署並嘗試連接器時,我可以正確地看到它。 但是,當我嘗試運行“Explore”時,我得到一個通用的“數據集配置錯誤 - Data Studio 無法連接到您的數據集”。 我正在按要求傳遞一組鍵值對……所以不確定發生了什么。

這是我在 getData function 中使用的代碼

function getData(request) {

  try {

    request.configParams = validateConfig(request.configParams);

    var requestedFields = getFields().forIds(
      request.fields.map(function(field) {
        return field.name;
      })
    );
    var data = JSON.parse(jsonSample).Table
    return {
      schema: requestedFields.build(),
      rows: data
    };

  }
  catch (e) {
    cc.newUserError()
      .setDebugText('Error fetching data from API. Exception details: ' + e)
      .setText(
        'The connector has encountered an unrecoverable error. Please try again later, or file an issue if this error persists.'
      )
      .throwException();
  }
}

其中 jsonSample 是一個文本字符串,包含以下 json(原始,未美化):

{
    "Table": [
        {
            "Entity": "Houston Heights",
            "EntityD": "",
            "Consolidation": "USD",
            "ConsolidationD": "United States of America, Dollars",
            "Scenario": "Actual",
            "ScenarioD": "",
            "Time": "2010M1",
            "TimeD": "Jan 2010",
            "View": "Periodic",
            "ViewD": "",
            "Account": "IFRS Balance Sheet",
            "AccountD": "",
            "Flow": "None",
            "FlowD": "",
            "Origin": "BeforeAdj",
            "OriginD": "",
            "IC": "None",
            "ICD": "",
            "UD1": "None",
            "UD1D": "",
            "UD2": "None",
            "UD2D": "",
            "UD3": "None",
            "UD3D": "",
            "UD4": "None",
            "UD4D": "",
            "UD5": "None",
            "UD5D": "",
            "UD6": "None",
            "UD6D": "",
            "UD7": "None",
            "UD7D": "",
            "UD8": "None",
            "UD8D": "",
            "CellValue": 2.25000000000000000000
        },
        {
            "Entity": "Houston Heights",
            "EntityD": "",
            "Consolidation": "USD",
            "ConsolidationD": "United States of America, Dollars",
            "Scenario": "Actual",
            "ScenarioD": "",
            "Time": "2010M1",
            "TimeD": "Jan 2010",
            "View": "Periodic",
            "ViewD": "",
            "Account": "IFRS Balance Sheet",
            "AccountD": "",
            "Flow": "None",
            "FlowD": "",
            "Origin": "BeforeAdj",
            "OriginD": "",
            "IC": "None",
            "ICD": "",
            "UD1": "Admin",
            "UD1D": "Admin",
            "UD2": "None",
            "UD2D": "",
            "UD3": "None",
            "UD3D": "",
            "UD4": "None",
            "UD4D": "",
            "UD5": "None",
            "UD5D": "",
            "UD6": "None",
            "UD6D": "",
            "UD7": "None",
            "UD7D": "",
            "UD8": "None",
            "UD8D": "",
            "CellValue": 2.240000000000000000000
        }
    ]
}

要解決這個問題,代碼應該是這樣的,因為你正在傳遞一個對象數組:

function getData(request) {
  try {
    request.configParams = validateConfig(request.configParams);

    var requestedFields = getFields().forIds(
      request.fields.map(function (field) {
        return field.name;
      })
    );

    var data = JSON.parse(jsonSample).Table;

    return {
      schema: requestedFields.build(),
      rows: data.map(function (row) {
        var values = [];

        requestedFields.asArray().forEach(function (field) {
          values.push(row[field.getId()]);
        });

        return { values: values };
      }),
    };
  } catch (e) {
    cc.newUserError()

      .setDebugText("Error fetching data from API. Exception details: " + e)

      .setText(
        "The connector has encountered an unrecoverable error. Please try again later, or file an issue if this error persists."
      )

      .throwException();
  }
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM