After a bit of work it is now possible to use sql to parse json using javascript!. I introduce to you version 0.1 of JsonSQL. Given a javascript object you can write a simple select statement to filter, order and limit the object:
jsonsql.query("select title,url from json.channel.items
where (category=='javascript' || category=='vista')
order by title,category asc limit 3",json);
Give it a shot. It would be nice to expand this a little more with a few aggregate functions (distinct, count, etc..).
Related posts:


3 Responses
Code Bus - Manipulating PHP arrays with SQL
22|May|2008 1[...] Richardson created a very small and simple JsonSQL library for JavaScript which allows you to run an extremely limited subset of SQL against a Json [...]
Matt
11|Aug|2009 2I am getting duplicate returns when using this.
I will show you the set up of my object, and maybe you can see where the issue is. I’ve checked my object and it is built correctly for how I need to use it.
My object is as follows
myObj = {
“elements”: [
{
"haschildren": "True",
"id": 1,
"name": "Element 1",
"children": [
{
"id": 25,
"name": "Element 1 Child 1"
},
{
"id": 26,
"name": "Element 1 Child 2"
},
{
"id": 27,
"name": "Element 1 Child 3"
}
]
},
{
“haschildren”: “False”,
“id”: 1,
“name”: “Element 1″,
“children”: []
}
]
}
There are 20 elements in my object. 5 have “haschildren” set to “True”, 15 have it set to “False” ( strings not boolean, I create this straight out of a DB ).
When I run:
jsonsql.parse(“select * from myObj.elements where (haschildren==’True’)”,myObj);
It returns 10 elements, exactly 2 of each.
I like the concept of this, but don’t have time to sit down and try to dig through it right now, but I figured I would let you know that there was a bug in this.
Thanks,
Matt
trent
11|Aug|2009 3Hey Matt,
Thanks for reporting. Looks like perhaps something with scoping, however I would need to look further into it. This project has kind of run dry since it didn’t pick up any steam when it was first started
Leave a reply