I am working on a service that transforms, translates and normalizes records received as semi-structured json. The requirements are as follows:
- The incoming json entities of the same type (type - person,
address etc.) may not to have the same same attributes.
- Some attributes may not be present in every entity of a given type.
- Attributes can be renamed.
- The incoming json entites are initially untyped. The type of the incoming entities can be determined by analyzing the available fields. So I imagine that rules are needed to reclassify entities to their Drools/Java class.
- It may not be possible to guarantee that the data in a given attribute is always of the same type (though everything can default to string).
Of course, these requirements are all the opposite of Java and comments in other posts (though a several years ago) have pointed out that it is difficult to process json with Drools.
Is there way to harmoniously apply Drools in the above scenario or are there minimal restrictions (aside from the obvious solution of imposing a strong data model) that would correct the situation?
I can think of a few approaches that might work for you:
- Parse your JSON facts into a memory structure (with something like Gson or Jackson), and insert those structures as facts into drools. Then it should be possible to write rules with LHS that can match the parsed facts. It would also be possible to update the facts through the Gson/Jackson API.
- It's possible to write drools facts directly in Java by creating instances of (if I recall) the RuleImpl class. You can then provide an arbitrary LHS that could parse/match arbitrary JSON however you'd like.