json path
See original GitHub issueHi, I am writing a custom operator to compare two arrays. I have a requirement where I want to select an array of strings from an array of objects in my fact (like json path $.phoneNumbers[*].type
).
{
"firstName": "John",
"lastName" : "doe",
"age" : 26,
"address" : {
"streetAddress": "naist street",
"city" : "Nara",
"postalCode" : "630-0192"
},
"phoneNumbers": [
{
"type" : "iPhone",
"number": "0123-4567-8888"
},
{
"type" : "home",
"number": "0123-4567-8910"
}
]
}
Expected output : ['iPhone', 'home']
My understanding is that selectn does not support this and I do not want to use dynamic facts to do this, since I want to keep the rules configurable. Is there a work around we can use?
One solution is to define json path in a fact as shown below and use https://www.npmjs.com/package/jsonpath to evaluate.
{
fact: 'person',
operator: 'containsAnyOf',
value: ['home', 'office'],
jsonPath: '.phoneNumbers[*].type'
}
Thanks
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
JSONPath Online Evaluator
An online playground for JSONPath. ... JSONPath Online Evaluator - jsonpath.com. JSONPath. Output paths. Expand JSONPath expressions ...
Read more >json-path/JsonPath - GitHub
A Java DSL for reading JSON documents. Build Status Maven Central Javadoc. Jayway JsonPath is a Java port of Stefan Goessner JsonPath implementation....
Read more >JSONPath Syntax | AlertSite Documentation
JSONPath is a query language for JSON, similar to XPath for XML. AlertSite API endpoint monitors let you use JSONPath in assertions to ......
Read more >Introduction to JsonPath - Baeldung
This article covered fundamental features of Jayway JsonPath — a powerful tool to traverse and parse JSON documents. Although JsonPath has some ...
Read more >JSONPath - XPath for JSON - stefan.goessner
JSONPath expressions always refer to a JSON structure in the same way as XPath expression are used in combination with an XML document....
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
Hi all,
just wanted to give a heads up that I’m actively working on this feature, in case anyone wanted to provide some early feedback on my plan of attack.
After surveying the options, it’s clear that
selectn
syntax is fundamentally unable to express the full breadth of object traversal use cases desired by users. As such, it’s in the best long term interest of this library to move forward with json-path as the primary supported syntax of thepath
feature (this is, after all, a json based library).deprecated support for selectn
I’m planning to deprecate support for
selectn
path syntax. If you want to continue using that format, you will have to installselectn
as a direct dependency of your application, and json-rules-engine will detect its presence, path syntax, and use it to parse relevant condition paths. This will be a breaking change because of the new requirement of addingselectn
as a direct dependency, but it won’t force anyone to convert all their rules before upgrading to v5. In fact, you can technically continue to use theselectn
syntax for old rules, and express json-path for new ones.By removing
selectn
as a dependency, we’ll save ~20% of the overall package size of json-rules-engine, and making the addition ofjsonpath-plus
fairly negligible (see below).which json-path library to use?
As discussed above, there was initial concern around having jsonpath as a dependency due its large package size. For this reason, I’m planning to use jsonpath-plus instead. This library checks several important boxes:
eta
Once the above research was done and the way forward was clear, this isn’t looking very difficult at all. Mainly I need to test a few cases and update the documentation, but I’ve got the proof of concept working.
@phstc Solving for the particular data structure you posted is likely do-able (the code change you suggested might work). The tricky part I think is solving this in a way that will support everyone else’s data structures as well. Although the most common case represented in this thread is an array of objects, there’s no limit to the ways a user may choose to structure their data.
I’m a bit short on time these days, but ultimately I’d like to solve this in a comprehensive way.