Is it possible to use JSONLogic as a query language?
See original GitHub issueIs it possible to use JSONLogic as a query language to filter JSON? Something like this:
var data =
[
{
"id": 1,
"name": "Oliver",
"email": "email1@mail.com"
},
{
"id": 2,
"name": "Jack",
"email": "email2@mail.com"
},
{
"id": 3,
"name": "Harry",
"email": "email3@mail.com"
},
{
"id": 4,
"name": "Jacob",
"email": "email4@mail.com"
},
{
"id": 5,
"name": "Charlie",
"email": "email5@mail.com"
},
{
"id": 6,
"name": "Thomas",
"email": "email6@mail.com"
},
{
"id": 7,
"name": "George",
"email": "email7@mail.com"
},
{
"id": 8,
"name": "Oscar",
"email": "email8@mail.com"
},
{
"id": 9,
"name": "James",
"email": "email9@mail.com"
},
{
"id": 10,
"name": "William",
"email": "email10@mail.com"
}
]
var filter =
{"or": [
{"and": [
{">=": [
{"var": "id"}, 3
]},
{"<=": [
{"var": "id"}, 5
]}
]},
{">": [
{"var": "id"}, 7
]}
]};
jsonLogic.filter(filter, data);
It should return:
[
{
"id": 3,
"name": "Harry",
"email": "email3@mail.com"
},
{
"id": 4,
"name": "Jacob",
"email": "email4@mail.com"
},
{
"id": 5,
"name": "Charlie",
"email": "email5@mail.com"
},
{
"id": 8,
"name": "Oscar",
"email": "email8@mail.com"
},
{
"id": 9,
"name": "James",
"email": "email9@mail.com"
},
{
"id": 10,
"name": "William",
"email": "email10@mail.com"
}
]
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
JsonLogic
JsonLogic isn't a full programming language. It's a small, safe way to delegate one decision. You could store a rule in a database...
Read more >JsonLogic | Hacker News
Instead of reinventing your own, using this language can be done, as long as it has a mapping into each database query language....
Read more >MistQL | MistQL
A query language for JSON-like structures. ... expressive enough, or readability of JSONLogic becomes difficult, then MistQL will probably work better.
Read more >Representing logic as data in JSON - Stack Overflow
But there are cases (like the example) where the former syntax can not ... The main work is done by the query defined...
Read more >JSON and Lisp-Like Queries | Skirtle's Den
MongoDB uses a JSON-based query syntax and the equivalent query would be: ... this makes it possible to replace the implicit field access...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You could do this by combining JavaScript’s filter with that rule:
You could also monkey-patch this onto the library quickly like:
By the way, there’s a special three-argument syntax for
<
and<=
that can simplify your rule:I hesitate to bake this into the spec, just because I don’t have time right now to write appropriate unit tests for it.
Does this get you where you need to go?
I think you’re right, you could pretty quickly patch in
replace
andadd
andremove
operations withadd_operation
that act directly on thedata
object.I’m not familiar with JsonPointer, but I have used dotty–I just suggested a way to mooch functionality off that library into JsonLogic rules in this issue.
Re: cleaner syntax without var, in your example how does the parser understand that “temp” and “pie.filling” are data names, but “apple” is a literal string? It’s totally doable in a command like
{"remove":"/foo"}
where that argument can’t be anything but a variable reference, but I don’t think it’ll work everywhere.