4

I have an existing jsonobject from the javax.json.JsonObject class.

I can't for the life of me figure out how I can modify the existing values in it. Ideally I'd like to do something like this:

if(object.getString("ObjectUUID").length()==0){
    object.put("ObjectUUID", UUID.randomUUID().toString());
}

According to the API you aren't allowed to modify that map.

http://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html

This map object provides read-only access to the JSON object data, and attempts to modify the map, whether direct or via its collection views, result in an UnsupportedOperationException.

Currently I'm getting around the problem with a quick hack but there must be a better solution than this:

if(object.getString("ObjectUUID").length()==0){
    JsonObjectBuilder job = Json.createObjectBuilder();
    job.add("ObjectUUID", UUID.randomUUID().toString());
    for(String key : object.keySet()){
        if(!key.equals("ObjectUUID")){
            job.add(key, object.get(key));
        }
    }
    object = job.build();
}

So the question how do you modify an existing jsonobject?

1
  • Did you find any solution for this? Commented Aug 4, 2016 at 8:57

3 Answers 3

4

Without knowing the structure of the JSON object, it's a bit difficult to provide an answer that addresses your specific problem. The JsonObject instance is immutable, so you can't update it like Jackson's ObjectNode. As you've probably found, the JsonObject.add() method is basically useless, and depending on the implementation, may yield an UnsupportedOperationException. It's puzzling why it exists in the first place.

The way to modify the object is to create a JsonObjectBuilder instance that wraps the original JsonObject. If the object instance was your original JsonObject you'd probably want to do something like this:

if(object.getString("ObjectUUID").length()==0){
    object job = Json.createObjectBuilder(object)
                     .add("ObjectUUID", UUID.randomUUID().toString())
                     .build();
}

This took me a while to understand myself, and it gets a but more verbose if you have nested objects, etc.. If you're updating or replacing an object or array, you don't need to explicitly remove it before adding. There's pros and cons of the approach, but it beats the monthly ceremony of updating Jackson on a monthly basis :)

Sign up to request clarification or add additional context in comments.

1 Comment

This should be the accepted answer. And yes, whoever decided to make JsonObject and JsonArray immutable (and rely on Builder pattern) in Jakarta EE JSON spec clearly did not think about such use cases... which is a pity.
1

I think there is no other way to modify a javax.json.JsonObject.

You should consider using jackson-databind.

Comments

0

GSON Guide;

obj.addProperty("Id", "001");

System.out.println("Before: " + obj.get("Id")); // 001

obj.addProperty("Id", "002");

System.out.println("After: " + obj.get("Id")); // 002

4 Comments

JsonObject doesn't have an addProperty method and you can't interact with the map directly using put, you will get an UnsupportedOperationException.
Thanks for the suggestion, doesn't directly answer the question but gson is a nice alternative. I'll try it out.
If you say i can explain with more. Good Luck.
This solution is for GSON, not JSON-P, which is where the javax.json.JsonObject class comes from. I'm surprised this was the accepted answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.