14

I have a String object containing some arbitrary json. I want to wrap it inside another json object, like this:

{
   version: 1,
   content: >>arbitrary_json_string_object<<
}

How can I reliably add my json string as an attribute to it without having to build it manually (ie avoiding tedious string concatenation)?

class Wrapper {
   int version = 1;
}

gson.toJson(new Wrapper())
// Then what?

Note that the added json should not be escaped, but a be part of the wrapper as a valid json entity, like this:

{
   version: 1,
   content: ["the content", {name:"from the String"}, "object"]
}

given

String arbitraryJson = "[\"the content\", {name:\"from the String\"}, \"object\"]";
3
  • Have you tried adding a content String field? I'm curious to see the result. Commented Dec 11, 2013 at 15:09
  • Probably the content will be escaped. Commented Dec 11, 2013 at 15:09
  • Did you solve your problem? Commented Dec 14, 2013 at 12:48

5 Answers 5

19

For those venturing on this topic, consider this.

A a = getYourAInstanceHere();
Gson gson = new Gson();
JsonElement jsonElement = gson.toJsonTree(a);
jsonElement.getAsJsonObject().addProperty("url_to_user", url);
return gson.toJson(jsonElement);
Sign up to request clarification or add additional context in comments.

1 Comment

This should be accepted answer! Also, if you wish to add a custom object make another JsonElement like JsonElement jsonElement2 = gson.toJsonTree(b); and add to the existing one, using: jsonElement.getAsJsonObject().add("other", jsonElement2);
8

Simple, convert your bean to a JsonObject and add a property.

Gson gson = new Gson();
JsonObject object = (JsonObject) gson.toJsonTree(new Wrapper());
object.addProperty("content", "arbitrary_json_string");
System.out.println(object);

prints

{"version":1,"content":"arbitrary_json_string"}

2 Comments

Does this work if the "arbitrary_json_string" is a JSON object?
@LutzHorn You can use JsonObject#add(JsonElement) for that.
6

This is my solution:

  Gson gson = new Gson();
  Object object = gson.fromJson(arbitraryJson, Object.class);

  Wrapper w = new Wrapper();
  w.content = object;

  System.out.println(gson.toJson(w));

where I changed your Wrapper class in:

// setter and getters omitted
public class Wrapper {
  public int version = 1;
  public Object content;
}

You can also write a custom serializer for your Wrapper if you want to hide the details of the deserialization/serialization.

Comments

2

You will need to deserialize it first, then add it to your structure and re-serialize the whole thing. Otherwise, the wrapper will just contain the wrapped JSON in a fully escaped string.

This is assuming you have the following in a string:

{"foo": "bar"}

and want it wrapped in your Wrapper object, resulting in a JSON that looks like this:

{
    "version": 1,
    "content": {"foo": "bar"}
}

If you did not deserialize first, it would result in the following:

{
    "version": 1,
    "content": "{\"foo\": \"bar\"}"
}

2 Comments

No, you won't have to. You can use the intermediary JsonObject that uses a LinkedTreeMap underneath.
@SotiriosDelimanolis: I do not think that does what I think OP is trying to achieve. I edited my answer to make my intentions more obvious.
1

If you don't care about the whole JSON structure, you don't need to use a wrapper. You can deserialize it to a generic json object, and also add new elements after that.

JsonParser parser = new JsonParser();
JsonObject obj = parser.parse(jsonStr).getAsJsonObject();
obj.get("version"); // Version field

Comments

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.