lloyd / JSONSelect

CSS-like selectors for JSON
http://jsonselect.org
ISC License
1.59k stars 116 forks source link

A means of filtering selections #18

Open lloyd opened 13 years ago

lloyd commented 13 years ago

Suggested by @dunkfordyce , implementation discussed here: http://librelist.com/browser//jsonselect/2011/6/3/objects/#b8bb946b3b839bd5894d9f78a5c6fb2fh

What if you want to limit the amount of data that's selected by your selector? A concrete case is when selecting a subset of twitter's api which return HUGE JSON OBJECTS with lots of stuff that YOU PROBABLY DONT CARE ABOUT

The proposal is to add two functions, one which discards items matching a selector, and the other which discards items not matching a selector. Naming is up in the air a bit, here are options:

These functions take a selector as an argument (like :has()), they do not affect the selection algorithm at all, but are applied after elements are selected and modify the return value.

for instance, given the document:

{
    "first": "Lloyd",
    "middle": "Trevor",
    "last": "Hilaiel"
}

both :root:only(.first,.last) and :root:remove(.middle) would return:

{ "first": "Lloyd", "last": "Hilaiel" }
rwaldron commented 13 years ago

Of the options above, I think that :include() and :exclude() are fairly comprehensive and semantically sound

lloyd commented 13 years ago

decisive. good.

dunkfordyce commented 13 years ago

just to mention: iv not given up the topic, im still in pondering mode! I think im more leaning to this sort of functionality in a differnt libray. ie, goiing back to my xpath/xslt comparison

studgeek commented 12 years ago

Also check out XQuery since it introduced the idea of modifying the result set over XPath.

Really once you start wanting to change the results you need something that templates the final result, like XSL and XQuery had. In JavaScript/JSON you can obviously use a literal for that. JSONiq shows some good examples of how it could look www.w3.org/2011/.../Documentation-0.1-JSONiq-Article-en-US.pdf