I'm trying to use the query engine SQLike and am struggling with the basic concept.
The JSON I'm using as my data source comes from my PHP code, like so:
var placesJSON=<? echo json_encode($arrPlaces) ?>;
Here's a sample JSON:
var placesJSON=[{"id":"100","name":"Martinique","type":"CTRY"},{"id":"101","name":"Mauritania","type":"CTRY"},{"id":"102","name":"Mauritius","type":"CTRY"},{"id":"103","name":"Mexico","type":"CTRY"},{"id":"799","name":"Northern Mexico","type":"SUBCTRY"},{"id":"800","name":"Southern Mexico","type":"SUBCTRY"},{"id":"951","name":"Central Mexico","type":"SUBCTRY"},{"id":"104","name":"Micronesia, Federated States","type":"CTRY"},{"id":"105","name":"Moldova","type":"CTRY"}];
I understand (via this reference) that I first need to unpack my JSON like so:
var placesData = SQLike.q(
{
Unpack: placesJSON,
Columns: ['id','name','type']
}
)
And the next step would be to query the results like so:
var selectedPlaces = SQLike.q(
{
Select: ['*'],
From: placesData,
OrderBy: ['name','|desc|']
}
Lastly, to display the results in the browser I should use something like:
document.getElementById("myDiv").innerHTML=selectedPlaces[0].name
This doesn't work. The error I get is: selectedPlaces[0].name is undefined.
I'm pretty sure I'm missing out on something very simple. Any hints?
"Unpack" converts an array of arrays, like
[["John", "Peterson", 38, 28000], ["Vicki", "Smith", 43, 89000]]
into an array of objects. Since your Json is already in this format, there's no need to unpack it.