I am calling a rest end point in Java using Unirest. The response is a JSON message which is complex and contains multiple levels inside.
I need to extract the value of a specific tag "eventDefinationId" from the response message. However, that is failing or not working. I cannot handle the JSON parsing from the response to extract the tag value
Below is the code I am using
//*Enrich Journey Event Info *//
Statement stmt_eventId = conn.createStatement();
ResultSet rs_event = stmt_eventId.executeQuery("SELECT JourneyName,definitionid FROM journeydetails");
while(rs_event.next()){
String JourneyName_For_EventId = rs_event.getString("JourneyName");
JourneyName_For_EventId = JourneyName_For_EventId.replaceAll(" ", "%20");
String Event_URL = "https://xxxxxxx.rest.marketingcloudapis.com/interaction/v1/interactions?name="+JourneyName_For_EventId+"&extras=all";
Unirest.setTimeouts(0, 0);
HttpResponse<String> response_event = Unirest.get(Event_URL)
.header("Content-Type", "application/json")
.header("Authorization", Token_Auth)
.asString();
String jsonString = response_event.getBody();
ObjectMapper mapper = new ObjectMapper();
com.fasterxml.jackson.databind.JsonNode actualObj = mapper.readTree(jsonString);
String eventDefinationId = actualObj.get("eventDefinitionId").textValue();
System.out.println(eventDefinationId);
}
Below is an example of a successful call using Postman. The image is the same API call, but using Postman instead of code.
As you see eventDefinationId is way below(inside metadata section) .
I want to extract it. I am getting all possible type of errors. Also this tag sometime may not be present. in that case it shouldn't error, but skip and go to next value from result set.
When I run the above program, I get null pointer exception
Exception in thread "main" java.lang.NullPointerException
at sfmc_process.ProcessJourney.UpdateJourneyDetailsTables(ProcessJourney.java:105)
at sfmc_main.AppLauncher.main(AppLauncher.java:29)
Command execution failed. org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1) at org.apache.commons.exec.DefaultExecutor.executeInternal (DefaultExecutor.java:404)
Line 105 which is error is : String eventDefinitionId = actualObj.get("eventDefinitionId").textValue();
Below is the image of the code which I am using. I want to extract the eventDefinitionId from message only.