views:

176

answers:

2

I'm looking for a JavaScript library that will allow me to query complex JSON objects using a LINQ-like syntax. A quick search found a couple of promising options that look they might offer what I need:

LINQ to JavaScript and jLinq

  • Does any one have any experience using them?
  • What are some pros and cons?
  • Is the performance comparable?
  • Does the function-passing syntax of LINQ to JavaScript offer any hidden benefits (I personally find the syntax of jLinq more appealing on first glance)?
  • What have you found lacking in either project?
  • Did you ever try contacting the authors? How responsive were they?
  • What project is more widely used?

EDIT:

Just saw this today: jsinq.

I think it will be the first one to get a thorough try-out.

A: 

The most basic and frequently used Linq operators are very commonly defined in widely used JS libraries. They just have different names (in fact, they have more traditional names than in Linq). Select becomes map, Where becomes filter, First and FirstOrDefault become [0].

Almost no library I know of (including I think the ones you linked to) bother to make the implementation lazy as in .NET Linq, they just evaluate immediately using arrays.

For a very nice, complete set of functional list operations, try: http://osteele.com/sources/javascript/functional/

Daniel Earwicker
Being familiar with LINQ, keeping as much of the syntax the same would be a huge help. The lazy loading is not a concern. This seems like it does have all the functionality I need, but I'll have to experiment some. I'm keeping the question open for now, in the hope someone WILL stumble by who has used the JavaScript LINQ libraries and share their experiences. After some time goes by, if nobody else does, I'll be sure to give you the answer.
Tom Tresansky
@Tom - I tend to be a bit wary of libraries that attempt to simulate the experience of using language X inside language Y if there is already a good way of doing the same thing in language Y. I think it's better to learn the "idioms" of language Y. People were writing functional sequence operators in JS before Linq came out, and in JS they kept the "traditional" Lispy names and approaches, so that's now the more JS style. So I say "When in Rome..." Even to the extent of how I lay out curly braces, which line up vertically in C#, but in JS and Java I put the first one on the same line, etc.
Daniel Earwicker
+3  A: 

Have you seen Rx for Javascript, yet? That's what you want.

Richard Hein
Thanks for the link, I hadn't seen this, and after viewing the project pages and videos for this and Rx I can already see where this could be handy.However, it is not what I'm looking for. I want to perform LINQ-like queries on JSON objects that have many levels of nesting.
Tom Tresansky
Yeah, you can do that with Rx - it's for querying any data or object in a reactive/asynch (i.e. AJAX-style) way, see an example of grabbing JSON data and then querying it using the Rx library here: http://codebetter.com/blogs/matthew.podwysocki/archive/2010/03/17/the-reactive-extensions-for-javascript-released.aspx
Richard Hein
I was going to suggest this but it's not really the same as linq to objects. (If it was, why would we need both Rx and Linq to Objects in .NET?) It works in the opposite direction to normal Linq in terms of the flow of data.
Daniel Earwicker
Everything you can do with a pull (IEnumerable) collection (including a single item in a collection) you can do with a push interface too (IObservable). That means LINQ queries. You just think in terms of events or pushed data, but you still query it the same way. For Javascript I imagine they thought System.Reactive was a higher priority in an AJAJ/AJAX world, maybe System.Interactive is coming later. You can do anything with Rx that you'd do with Linq to Objects you just work with data (or computations) being pushed to you instead of you pulling them.
Richard Hein