sreisman sreisman - 1 year ago 83
JSON Question

Update JSON values with lambda

I would like to take a JsonNode tree and update each of the values. Can this be done with a lambda expression?

"resource": {
"key1": "value1",
"key2": "value2",
"key3": "value3"

I am trying to use the following logic:

JsonNode json = new ObjectMapper().readTree(new ObjectMapper().writeValueAsString(myObject));

ObjectNode rootNode = (ObjectNode) json;
ObjectNode resourceNode = (ObjectNode) rootNode.path("resource");

//Psuedo Code here
resourceNode.forEach(node -> node.value = myFunc(node.value));

Thanks in advance!

Answer Source

Use .fields() to iterate over the key/value pairs:

    entry -> resourceNode.set(
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download