How to retrieve Deployed DMN as JSON,By Java or Programmatically

Hi,
I am new to flowable, I used the below Java code to create and deploy the DMN Programmatically,
But i don’t know how to retrieve the same as JSON ( In DB JSON is stored as XML for my requirement i need to retrieve as JSON),
Is there any way to retrieve the deployed DMN as JSON, Can anyone please help me here ?

String jsonStr = "{\"key\":\"pravinTest\",\"name\":\"Pravin Test\",\"hitIndicator\":\"FIRST\",\"inputExpressions\":[{\"entries\":null,\"id\":\"input1\",\"label\":\"Input 1\",\"type\":\"string\",\"variableId\":\"input1\",\"variableType\":\"variable\"}],\"outputExpressions\":[{\"entries\":null,\"id\":\"user\",\"label\":\"User\",\"type\":\"string\",\"variableId\":\"user\",\"variableType\":\"variable\"}],\"rules\":[{\"input1\":\"A\",\"user\":\"pravin\"}]}";
JsonNode JsonNode = OBJECT_MAPPER.readTree(jsonStr);
DmnDefinition dmnDefinition = new DmnJsonConverter().convertToDmn(JsonNode, "pravinTest", 1, new Date());
DmnRepositoryService dmnRepositoryService = dmnEngine.getDmnRepositoryService();
DmnDeployment dmnDeploymentId = dmnRepositoryService.createDeployment().name("pravinTest")
				.category("Pravin-Test").addDmnModel("pravinTest.dmn", dmnDefinition).deploy();

Hi @pravinpsa

Just to be clear. It’s good to know the difference between the JSON and XML models.
The JSON represents the DecisionTable (or DecisionService) in the DecisionTable (or Service) Editor.
When deployed this editor model is transformed to DMN XML (conforming to the DMN spec).

So when you want to create the DMN model programatically it is not necessary to first create the JSON (editor) model. You can omit that step and create the DMN model directly.

But if there is somehow the requirement to transform the DMN XML to the Editor JSON this can be done like this.

  1. Get the DmnDecision; f.e. DmnRepositoryService.getDmnDefinition(String decisionId)
  2. Convert to JSON; DmnJsonConverter.convertToJson(DmnDefinition model)

Hope this helps.

Regards,

Yvo

Hi @yvo ,

Thanks for you comment , I tried the above snippet to get Deployed DMN but am getting Exception, am using spring boot app, am i missing any thing ?

@Autowired
DmnEngine dmnEngine;

@Autowired
DmnRepositoryService dmnRepositoryService;

DmnDefinition dmnDefinitionTest = dmnRepositoryService.getDmnDefinition("pravinTest");

org.flowable.common.engine.api.FlowableObjectNotFoundException: no decision found with id 'pravinTest'

It seems you are querying on the key.
For that you can create a DecsionQuery on key.

  DmnDecision decision = repositoryService.createDecisionQuery()
          .latestVersion()
          .decisionKey("pravinTest")
          .singleResult();

Please have a look at the documentation for more info.

Regards,

Yvo

Hi @yvo ,

Thanks for the solution, It worked !

DmnDecision decision = dmnRepositoryService.createDecisionQuery().decisionKey("pravinTest").latestVersion().singleResult();
DmnDefinition outputDmnDefinition = dmnRepositoryService.getDmnDefinition(decision.getId());
JsonNode outputJsonNode = new DmnJsonConverter().convertToJson( outputDmnDefinition );
1 Like