Json-mask

json-mask Tiny language and engine for selecting specific parts of a JS object, hiding the rest.

The main difference between JSONPath / JSONSelect and this engine is that JSON Mask preserves the structure of the original input object.

It's complier uses a cute grammar syntax:

1
2
3
4
5
 Props ::= Prop | Prop "," Props
   Prop ::= Object | Array
 Object ::= NAME | NAME "/" Object
  Array ::= NAME "(" Props ")"
   NAME ::= ? all visible characters ?

Translating it few examples, that are loosely based on XPath syntax:

  • a,b,c comma-separated list will select multiple fields
  • a/b/c path will select a field from its parent
  • a(b,c) sub-selection will select many fields from a parent
  • a/*/c the star * wildcard will select all items in a field

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
> var jmask = require('json-mask');

> var data = {name: {first: 'hemanth', last:'hm'}, age:7}

> jmask(data,'name')
{ name: { first: 'hemanth', last: 'hm' } }

> jmask(data,'name/first')
{ name: { first: 'hemanth' } }

> jmask(data,'name/last')
{ name: { last: 'hm' } }

> jmask(data,'name/first,age')
{ name: { first: 'hemanth' }, age: 7 }

GIF FTW!

json-mask

Special thanks to the author Yuriy Nemtsov for a wonderful module!

Suggest a module

Comments