9

I'm currently working on a project where I'm dealing with a fair amount of JSON data being transmitted backwards and forwards and stored by the browser as lists of javascript objects. For example:

person: {
   // Primary Key
   key: "id",
   // The actual records
   table: {
       "1": {id: 1, name: "John", surname: "Smith", age: 26},
       "2": {id: 2, name: "Mary", surname: "Brown", age: 19},
       // etc..
   },
   indexes: {
       // Arrays of pointers to records defined above
       "name": [
            {id: 89, name: "Aaron", surname: "Jones", age: 42},
            // etc..
       ]
   }

I'm finding myself coding all sorts of indexing and sorting algorithms to efficiently manipulate this data and I'm starting to think that this kind of thing must have been done before.

I have experience of using the Ext.data.Store and Ext.data.Record objects for performing this kind of data manipulation, but I think they are overly complex for junior developers and the project I'm working on is a small mobile application where we cant afford to have a 300K+ library added just for the sake of it, so I need something really minimal.

Any ideas if there is a Javascript JSON manipulation framework that has the following:

  1. Can store,
  2. retrieve,
  3. sort,
  4. and iterate through JSON data,
  5. with a clean API,
  6. minimal performance drag (Mobiles dont have a lot of computing power)
  7. and a small payload that is ideally <10K?

I might be asking for too much, but hopefully someone's used something like this... The kind of thing I'm looking for the is the JSON equivalent of jQuery, maybe its not so outlandish.

Steven de Salas
  • 20,944
  • 9
  • 74
  • 82

3 Answers3

12

Take a look on jsonQ

It fullfill all the requirement pointed on question.

  • Can store,
  • retrieve
  • and iterate through JSON data,

Provide traversing (like find,siblings, parent etc) and manipulation method like(value, append, prepend);

  • sort

Provide a direct array sort method and sort method which run on jsonQ object. (Both sort method run recursively)

  • with a clean API

Its a trial to have same API for JSON as jQuery DOM APIs . So if you are familiar with jquery. Its easy to catch up. More over complete documentation of apis is available.

  • minimal performance drag

It create a new format on initialization of jsonQ for a JSON data which is used internally to traverse data which are more efficient. (Its like having all the loops at once, so you don't have to make loop over loop to iterate each time that json).

  • and a small payload that is ideally <10K?

minified version is 11.7 kb.

flydev
  • 4,327
  • 2
  • 31
  • 36
Sudhanshu Yadav
  • 2,323
  • 18
  • 18
0

Actually your question is not good, I suppose. From your example one could see, that you're trying to emulate SQL-like storage with JSON. Maybe, you just need to take IndexedDB?

jsonpath matches points 4-7 (and maybe 3) of your exact requirements and JSON global object allows 1 and 2 with just once call for each.

Also IMHO requirements are unreal, especially with the last one about size.

kirilloid
  • 14,011
  • 6
  • 38
  • 52
  • Hi Kirilloid, thanks for your response. IndexedDB has a very complex API, and it requires disk IO which is too much of a performance issue. The app is a single-page web app, so in-memory JSON provides by far the best performance. I agree the size requirement is quite limiting, but I dont think its impossible given that I'm only after the methods .get(), .set(), .index(), .each() and .data property for keeping track of the JSON.. I'll take a look at jsonpath and let you know. – Steven de Salas Mar 16 '12 at 11:48
  • jsonpath is interesting, I like the idea of trying to emulate XPath, but I'm not sure its what I'm after since XPath is for un-structured documents and I'm more after in-memory SQL-like storage like you mentioned (where there is a data model and distinct entities). – Steven de Salas Mar 16 '12 at 12:12
  • 1
    Xpath is for structured documents too, but for **tree**-structured ;-) – kirilloid Mar 16 '12 at 12:22
0

I think Lawnchair is something you're looking for. Its homepage says it is made with mobile in mind, however I've not used it before so I cannot comment on that.

It is simple key-value store, although you can define your own indexes, something like with CouchDB. I've not seen support for selectors, however there is a query plugin available, promising easy selection.

Something like jQuery relies on sizzle, which is CSS selector library, that isn't applicable in your case. XPath is in my opinion your best bet, since it's used primarily with XML, a tree structure. Since JSON object can be represented as a tree, I've found JSONSelect, a small library that supports JSON Xpath selectors in 'CSS-ish' way.

If you'd be able somehow to plug JSONSelect into Lawnchair, I think you've found a great combination :)

usoban
  • 5,428
  • 28
  • 42