question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to use JSONLogic as a query language?

See original GitHub issue

Is 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:open
  • Created 7 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
jwadhamscommented, Nov 3, 2016

You could do this by combining JavaScript’s filter with that rule:

data.filter(function(item){
  return jsonLogic.apply(filter, item);
})

You could also monkey-patch this onto the library quickly like:

jsonLogic.filter = function(rule, data){
    if(typeof data.filter !== "function"){
        throw "jsonLogic.filter requires data be an Array";
    }
    return data.filter(function(item){
        return jsonLogic.apply(rule, item);
    });
};

By the way, there’s a special three-argument syntax for < and <= that can simplify your rule:

{ "or": [
    { "<=": [ 3, { "var": "id" }, 5 ] },
    { ">": [ { "var": "id" }, 7 ] }
] };

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?

1reaction
jwadhamscommented, Nov 10, 2016

I think you’re right, you could pretty quickly patch in replace and add and remove operations with add_operation that act directly on the data 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found