Add property to json string with jackson json

I am saving a json string in a text box in mysql. After insertion, I want to update the json string and add the mysql string id to it using jackson json.

I have a java string which is in Json format

{ "thing":"val" } 

I want to add another K / V without writing lines of code.

to finally do this:

 { "thing":"val" "mysqlId":10 } 

I can convert my String to JsonNode:

 ObjectMapper mapper = new ObjectMapper(); JsonNode json = mapper.readTree( jsonStr); 

Looking for something like this

 json.put("mysqlId",10); json.toString(); 

then update the json new line in mysql in the text box

I can not do it. I do not want to use many classes, is there an easy way to do this with jackson?

+7
source share
1 answer

Try adding JsonNode to com.fasterxml.jackson.databind.node.ObjectNode , and then call put set (or replace ) on it.

+23
source

All Articles