toto toto - 4 years ago 132
JSON Question

jQuery JSON Parse - Unexpected Token Error with getjson

I'm trying to parse json data from a api feed but can't get get json to parse accordingly the array.

I fairly new to this so please bare with me.

So here's my json file, there is more to it but I didnt want to post the entire file.

json location
https://cbpfapi.unocha.org/vo1/odata/Poolfund

{
"odata.metadata": "https://cbpfapi.unocha.org/vo1/odata/$metadata#Poolfund",
"value": [{
"Id": 23,
"PoolfundName": "Afghanistan",
"PoolfundCodeAbbrv": "AFG23",
"Latitude": "34.53333300",
"Longitude": "69.16666700",
"CountryCode": "AF"
}, {
"Id": 17,
"PoolfundName": "CAR",
"PoolfundCodeAbbrv": "CAR17",
"Latitude": "4.36122000",
"Longitude": "18.55496000",
"CountryCode": "CF"
}, {
"Id": 24,
"PoolfundName": "DRC",
"PoolfundCodeAbbrv": "DRC24",
"Latitude": "-4.32758000",
"Longitude": "15.31357000",
"CountryCode": "CD"
}, {
"Id": 15,
"PoolfundName": "Sudan",
"PoolfundCodeAbbrv": "SUD15",
"Latitude": "15.55177000",
"Longitude": "32.53241000",
"CountryCode": "SD"
}, {
"Id": 21,
"PoolfundName": "Somalia",
"PoolfundCodeAbbrv": "SOM21",
"Latitude": "2.03333300",
"Longitude": "45.35000000",
"CountryCode": "SO"
}, {
"Id": 19,
"PoolfundName": "South Sudan",
"PoolfundCodeAbbrv": "SSD19",
"Latitude": "4.85165000",
"Longitude": "31.58247000",
"CountryCode": "SS"
}, {
"Id": 52,
"PoolfundName": "Colombia",
"PoolfundCodeAbbrv": "COL52",
"Latitude": "4.59805600",
"Longitude": "-74.07583300",
"CountryCode": "CO"
}, {
"Id": 53,
"PoolfundName": "Ethiopia",
"PoolfundCodeAbbrv": "ETH53",
"Latitude": "8.98060340",
"Longitude": "38.75776050",
"CountryCode": "ET"
}, {
"Id": 54,
"PoolfundName": "Haiti",
"PoolfundCodeAbbrv": "HTI54",
"Latitude": "18.53333300",
"Longitude": "-72.33333300",
"CountryCode": "HT"
}, {
"Id": 59,
"PoolfundName": "Myanmar",
"PoolfundCodeAbbrv": "MMR59",
"Latitude": "19.74500000",
"Longitude": "96.12972000",
"CountryCode": "MM"
}, {
"Id": 60,
"PoolfundName": "Pakistan",
"PoolfundCodeAbbrv": "PAK60",
"Latitude": "33.72938820",
"Longitude": "73.04329000",
"CountryCode": "PK"
}, {
"Id": 64,
"PoolfundName": "Yemen",
"PoolfundCodeAbbrv": "YEM64",
"Latitude": "15.35202900",
"Longitude": "44.20745600",
"CountryCode": "YE"
}, {
"Id": 67,
"PoolfundName": "oPt",
"PoolfundCodeAbbrv": "PSE67",
"Latitude": "31.89964000",
"Longitude": "35.20422000",
"CountryCode": "PS"
}, {
"Id": 70,
"PoolfundName": "Turkey",
"PoolfundCodeAbbrv": "TUR70",
"Latitude": "41.01384000",
"Longitude": "28.94966000",
"CountryCode": "TR"
}, {
"Id": 71,
"PoolfundName": "Lebanon",
"PoolfundCodeAbbrv": "LBN71",
"Latitude": "33.88894000",
"Longitude": "35.49442000",
"CountryCode": "LB"
}, {
"Id": 73,
"PoolfundName": "Jordan",
"PoolfundCodeAbbrv": "JOR73",
"Latitude": "31.95522000",
"Longitude": "35.94503000",
"CountryCode": "JO"
}, {
"Id": 62,
"PoolfundName": "Syria",
"PoolfundCodeAbbrv": "SYR62",
"Latitude": "33.51020000",
"Longitude": "36.29128000",
"CountryCode": "SY"
}, {
"Id": 72,
"PoolfundName": "Iraq",
"PoolfundCodeAbbrv": "IRQ72",
"Latitude": "33.31711800",
"Longitude": "44.36323700",
"CountryCode": "IQ"
}]
}


Here's my code:

(function() {
// Create the connector object
var myConnector = tableau.makeConnector();

// Define the schema
myConnector.getSchema = function(schemaCallback) {
var cols = [{
id: "Id",
alias: "Id",
dataType: tableau.dataTypeEnum.string
}, {
id: "PoolfundName",
alias: "PoolfundName",
dataType: tableau.dataTypeEnum.string
}, {
id: "PoolfundCodeAbbrv",
alias: "PoolfundCodeAbbrv",
dataType: tableau.dataTypeEnum.string
}, {
id: "Latitude",
alias: "Latitude",
dataType: tableau.dataTypeEnum.float
}, {
id: "Longitude",
alias: "Longitude",
dataType: tableau.dataTypeEnum.float
}, {
id: "CountryCode",
alias: "CountryCode",
dataType: tableau.dataTypeEnum.string
}];

var tableSchema = {
id: "CBPFPoolfund",
alias: "CBPF Poolfund API",
columns: cols
};

schemaCallback([tableSchema]);
};

// Download the data
myConnector.getData = function(table, doneCallback) {
$.getJSON("https://cbpfapi.unocha.org/vo1/odata/Poolfund" + "?callback=?", function(resp) {
var val = resp.value,
tableData = [];

// Iterate over the JSON object
for (var i = 0, len = val.length; i < len; i++) {
tableData.push({
"Id": val[i].properties.Id,
"PoolfundName": val[i].properties.PoolfundName,
"PoolfundCodeAbbrv": val[i].properties.PoolfundCodeAbbrv,
"Longitude": val[i].properties.coordinates[0],
"Latitude": val[i].properties.coordinates[1],
"CountryCode": val[i].properties.CountryCode,
});
}

table.appendRows(tableData);
doneCallback();
});
};

tableau.registerConnector(myConnector);

// Create event listeners for when the user submits the form
$(document).ready(function() {
$("#submitButton").click(function() {
tableau.connectionName = "CBPF Poolfund API"; // This will be the data source name in Tableau
tableau.submit(); // This sends the connector object to Tableau
});
});
})();


Thanks!

Answer Source

I found some problems in your code inside myConnector.getData:

  1. this url https://cbpfapi.unocha.org/vo1/odata/Poolfund?callback=? is causing a unexpected error. Removing the ?=callback=? solved this issue.
  2. you're trying to get the values using .properties.keyName. You don't need .properties. Just use: obj.keyName to access an object key.
  3. val[i].coordinates[0] and val[i].coordinates[1] doesn't exist in your data set. It's val[i].Longitude and val[i].Latitude.

A working code:

$.getJSON("https://cbpfapi.unocha.org/vo1/odata/Poolfund", function(resp) {
    var val = resp.value,
    tableData = [];

    // Iterate over the JSON object
    for (var i = 0, len = val.length; i < len; i++) {
        tableData.push({
            "Id": val[i].Id,
            "PoolfundName": val[i].PoolfundName,
            "PoolfundCodeAbbrv": val[i].PoolfundCodeAbbrv,
            "Longitude": val[i].Longitude,
            "Latitude": val[i].Latitude,
            "CountryCode": val[i].CountryCode,
        });
    }

    table.appendRows(tableData);
    doneCallback();    

});

Fiddle here: https://jsfiddle.net/mrlew/mvmmppng/3/ (populated a table with the data)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download