You could use Jackson, that with Maven can be added to pom specifying the following dependency:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
With Jackosn you can use the ObjectMapper (import it with import com.fasterxml.jackson.databind.ObjectMapper;
). So if you have an object obj
of type MyClass, you can do as follow:
MyClass objToCovert = new MyClass();
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(obj);
The json must be passed to your method that updates the Json structure to patch. As an Example of a method that can do the update I report the following (tag
is the key):
public static JsonStructure updateKeyValue(JsonStructure jsonStructure, String tag, String value) {
JsonPointer jsonPointer = Json.createPointer("/" + tag);
JsonString jsonStringValue = (JsonString) jsonPointer.getValue(jsonStructure);
jsonStructure = jsonPointer.replace(jsonStructure, jsonStringValue);
return jsonStructure;
}