JSON Mask and the Partial Response Middleware (a la Google APIs)

332 views
Skip to first unread message

Yuriy Nemtsov

unread,
Jul 15, 2013, 2:59:11 AM7/15/13
to nod...@googlegroups.com
If you send the "?fields=" query-string to a Google API, you'll get just the parts of the JSON object from the response that you requested.

Now, you can have this same exact feature in your Node.js services. Say hello to JSON Mask and the Partial Response Middleware for express!

JSON Mask is a tiny language and an engine for selecting specific parts of a JS object, hiding/masking the rest. Here's a tiny sample of what it looks like:
var mask = require('json-mask')
mask({p: {a: 1, b: 2}, z: 1}, 'p/a,z')  // {p: {a: 1}, z: 1}
Partial Response Middleware is an express middleware that will integrate with your existing services with no additional code if you're using res.json() or res.jsonp() So, instead of getting the entire JSON object, your services will start being able to respond with partial objects.


I'd love to hear what you think about these.

- Yuriy

Floby

unread,
Jul 16, 2013, 6:13:52 AM7/16/13
to nod...@googlegroups.com
Looks nice. But why not choose something like JSONPath for selecting fields ?

Scott Rahner

unread,
Jul 16, 2013, 9:08:00 AM7/16/13
to nod...@googlegroups.com
Awesome! I have always loved the gdata partial handling. Code looks sweet too, good call separating the mask and the middleware. This is going to save the tubes from lots of useless data =D

Yuriy Nemtsov

unread,
Jul 16, 2013, 2:00:25 PM7/16/13
to nod...@googlegroups.com
Great question. A few reasons:

    1. JSON Mask doesn't alter the structure of the document. It doesn't select elements (like JSONPath or JSONSelect), but rather filters the parts of the object that you don't need. To illustrate that point, instead of getting "[{a: 1}, {z: 1}]" from the example above, with JSON Mask you get "{p: {a: 1}, z: 1}". This is important to me because I made this specifically to work with HTTP resources, the structure of which I didn't want to change.

    2. It's faster for the types of selectors it supports (the syntax is simple, so there are only a handful). Take a look at http://jsperf.com/jsonpath-vs-jsonmask (61% faster on selecting items from an array in Chrome). It can be even faster if you pre-compile the query with `jsonMask.compile()` and then use `jsonMask.parse()`. But that, of course, only works when you know the query ahead of time.

    3. It has a scary mascot :)

- Yuriy
Reply all
Reply to author
Forward
0 new messages