I've been attempting to achieve the following outcome using external JSON data but haven't had any luck finding a solution on search engines or forums. Can anyone provide assistance with this? Thank you in advance.
people =
[{id: 1, name: "Tom", carid: 1},
{id: 2, name: "Bob", carid: 1},
{id: 3, name: "Sir Benjamin Rogan-Josh IV", carid: 2}];
cars=
[{id: 1, name: "Ford Fiesta", color: "blue"},
{id: 2, name: "Ferrari", color: "red"},
{id: 3, name: "Rover 25", color: "Sunset Melting Yellow with hints of yellow"}];
var res = alasql('SELECT people.name AS person_name, cars.name, cars.color \
FROM ? people LEFT JOIN ? cars ON people.carid = cars.id',[people, cars]);
Result:
[{"person_name":"Tom","name":"Ford Fiesta","color":"blue"},{"person_name":"Bob","name":"Ford Fiesta","color":"blue"},{"person_name":"Sir Benjamin Rogan-Josh IV","name":"Ferrari","color":"red"}]
This is how I'm trying to access external JSON files:
people.json
[{id: 1, name: "Tom", carid: 1},{id: 2, name: "Bob", carid: 1}, {id: 3, name: "Sir Benjamin Rogan-Josh IV", carid: 2}]
cars.json
[{id: 1, name: "Ford Fiesta", color: "blue"},{id: 2, name: "Ferrari", color: "red"},{id: 3, name: "Rover 25", color: "Sunset Melting Yellow with hints of yellow"}]
JS Code
$(document).ready(function() {
$.getJSON('http://example.com/people.json', function(data) {
people = JSON.parse(data);
});
$.getJSON('http://example.com/cars.json', function(data) {
cars = JSON.parse(data);
});
var res = alasql('SELECT people.name AS person_name, cars.name, cars.color \
FROM ? people LEFT JOIN ? cars ON people.carid = cars.id',[people, cars]);
document.getElementById('id01').textContent = JSON.stringify(res);
}):
I'm encountering the following error in the console
Uncaught SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)
at Object.success (list.html:26)
at c (jquery.min.js:2)
at Object.fireWith [as resolveWith] (jquery.min.js:2)
at l (jquery.min.js:2)
at XMLHttpRequest.<anonymous> (jquery.min.js:2)