Skip to content
Advertisement

How to properly convert HashMap<String,List> to Json with array of json object

I have a stream of Kafka messages and wanted to build a HashMap<String,List<Object>> to be used as API response in Json format.

JavaScript

expected response:

JavaScript

actual response:

JavaScript

Advertisement

Answer

It is now working using these changes:

JavaScript
  1. Convert Kafka message string to JSONObject new JSONObject(consumerRecord.value())
  2. Construct a JSONObject from a Map using jsonObject = new JSONObject(responses);
  3. Return Map<String, Object> using jsonObject.toMap();
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement