Proposal for adding JSON/array matching to JSONQuery

33 views
Skip to first unread message

NicVolanschi

unread,
May 16, 2009, 5:12:35 PM5/16/09
to json-query
Hi all,

First of all, let me say that I find the current JSONQuery an
excellent idea. Really great work!
Now I studied the requirements on the wiki, and it seems that one of
the missing but desired features is some matching operator for arrays,
right?

I have just finished writing a library called myPatterns [1] for
pattern matching of JavaScript data using either patterns written in
JSON syntax (e.g.: "{a:%x, b:[2, 3, %y]}") or in custom syntaxes that
can be easily defined by any programmer.

I think that integrating such pattern matching features in JSONQuery
not only answers the above requirement, buy also increases the power
and comfort of the queries.

So I have built this week a patched version [2] of JSONQuery adding
two new operators:
- [~?"pat"] to select from an array all the items matching the pattern
"pat"
- [~="pat"] to extract, from all the items in an array matching the
pattern "pat", the sub-data matched by the variables in the pattern.

For instance the query:
[~?"[%x|%y]"]
extracts from an array all non-empty sub-arrays (the "[%x|%y]"
notation means an array whose first element is x and whose rest is y),
and is equivalent to the following query using only existing
operators:
[?(@[0]!=undefined)]

Likewise, the query:
[~="[%x|%y]"]
both filters the non-empty sub-arrays and extracts from each of them
the x and y sub-data, and is equivalent to the following, more
verbose, standard query:
[?(@[0]!=undefined)][={x:@[0], y:@.slice(1)}]

What do you guys thinks about that?
Cheers,
Nic.

[1] http://mypatterns.free.fr/
[2] A more complete set of examples, as well as the patched version
prototype can be found at http://mypatterns.free.fr/JS/dev/mypatterns/ex/jsonquery.html
Reply all
Reply to author
Forward
0 new messages