Important JSON/Json Conversion Tricks
To convert Jsonstring into Map<String,Object> we can use the following code:
String msg="{"id":"123"}"; Map<String, Object> inputEvent = (Map<String, Object>) ESMapper .jsonToMap(new JsonParser().parse(msg).getAsJsonObject()); //Another Method Map<String, Object> inputEvent = new ObjectMapper().readValue(msg, Map.class);
To read from the file and converting it into JSONObject and setting up the value in it we use following code:
/*Contents of the json file */ { "Code": "Event", "id": "order", "randomId": "14c42-526af7764f64", "timestampEventOccurred": "2019-06-26T13:40:16+0000", "timestampEventReceived": "2019-06-26T13:40:16+0000" } String content = getJsonFromFile("data/jsonfile.json"); JSONObject jsonObject = new JSONObject(content); jsonObject.setValue("tenantId", id); String newcontent = jsonObject.toString();