简体   繁体   中英

Google Data Studio - Custom Connector - Get Data parsing json

I'm trying to build a custom connector on GDS for an external API.

I've configured the schema with all the fields coming from the API and I can see that correctly when I deploy and try the connector. However when I try to run the "Explore" I get a generic "Data Set Configuration Error - Data Studio cannot connect to your data set". I am passing an array of key value pairs as requested... so not sure what's going on.

This is the code I am using in the 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();
  }
}

Where jsonSample is a text string contain the following json (raw, not beautified):

{
    "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
        }
    ]
}

To solve this issue, the code should be like this because you are passing an array of objects:

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();
  }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM