Reputation: 13
I have a use case where I have to create a JSON object with place holders in my java application. The following JSON object has attribute1 and attribute3 required to resolved in my other microservice that has a register of these placeholders.
{
"Attribute1": "myPlaceholder1"
"Attribute2": "Value of Attribute 2"
"Attribute3": "myPlaceholder2"...
}
I have a registry with the corresponding placeholder values to replace.
Place holder id | Place holder value |
---|---|
myPlaceholder1 | U1 |
myPlaceholder2 | U2 |
Is there a java/JSON library that allows me to define placeholders and allow me to replace these values at runtime?
Thanks!
Upvotes: 0
Views: 280
Reputation: 315
Here is my solution for your problem:
try {
String input = "{\"attr1\":\"value1\", \"attr2\":\"value2\"}";
Map<String, String> data = new HashMap<>();
// this mapper is an instance of ObjectMapper from Spring (jackson library)
data = mapper.readValue(input, data.getClass());
System.out.println(data.toString());
// here you can manipulate data and serialize it to string again if needed
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Take a note that the readValue has a warning but you can ignore it by SuppressWarning("unchecked").
Upvotes: 1