How to rename a field in JsonNode using Jackson API

I have a JsonNode with this JSON in it:

{"temperature":17,"long":200,"lat":100}

I want to change JsonNode to look like this

{"MyNewFieldName":17,"long":200,"lat":100}

Is it possible to use the Jackson API?

+4
source share
1 answer

You cannot rename keys into JSON pairs with the key. You will need to create a new key-value pair with the same value, but with a different key and delete the old one.

JsonNode node = ...;
ObjectNode object = (ObjectNode) node;
object.set("MyNewFieldName", new TextNode(node.get("temperature").asText()));
object.remove("temperature");
+6
source

Source: https://habr.com/ru/post/1544061/


All Articles