11

I have a JsonNode with this JSON in it :

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

I want to change the JsonNode to look like this

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

Is it possible using Jackson API ?

hich
  • 133
  • 1
  • 1
  • 12

1 Answers1

17

You won't be able to rename keys in key-value JSON pairs. What you will need to do is create a new key-value pair with the same value but with a different key and remove the old one.

JsonNode node = ...;
ObjectNode object = (ObjectNode) node;
object.set("MyNewFieldName", node.get("temperature"));
object.remove("temperature");
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724