Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to rename a field in a JsonNode using jackson API

Tags:

java

json

jackson

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 ?

like image 897
hich Avatar asked Jun 10 '14 21:06

hich


1 Answers

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");
like image 170
Sotirios Delimanolis Avatar answered Sep 19 '22 17:09

Sotirios Delimanolis