Below is my code in that I am trying to set to a value in JSON format:
{"Details": "{\"user\":\"user1\",\"password\":\"1234\"}"}
Here, I have to set the data in user and pass but its in double quote ("").
I have tried a path with detail.user but it's not working:
ObjectMapper mapper = new ObjectMapper();
ObjectNode node = (ObjectNode) mapper.readTree(new File(templatePath));
// System.out.println(node);
Configuration config = Configuration.builder()
.jsonProvider(new JacksonJsonNodeJsonProvider())
.mappingProvider(new JacksonMappingProvider()).build();
json = JsonPath.using(config).parse(node);
for (int i = 0; i < list.size(); i++) {
String x = list.get(i);
arr = x.split(": ");
String newHeader = arr[0].replace("|", ".");
if (newHeader.contains("[")) {
String nHeader = "$." + newHeader;
String actualVal;
if (arr.length >= 2) {
actualVal = arr[1];
} else {
actualVal = "";
}
json.set(nHeader, actualVal).jsonString();
} else {
String actualVal;
if (arr.length >= 2) {
actualVal = arr[1];
} else {
actualVal = "";
}
json.set(newHeader, actualVal).jsonString();
}
}
I have tried with the code above to set the data. But I am getting an Exception.
Take the following code as reference and try to update your object. You can use gson or jackson to play with the JSON objects. Please do some handful of work before posting a question.