3

I have this json string

{
  "team": "xyz",
  "patern": "abc",
  "service": "{\"version\":0,\"name\":\"some_service_name\"}",
  "op": "{\"version\":0,\"name\":\"some_op_name\"}",
  .
  .
  .
}

and would like to convert it into JsonObject, since it has a json string inside I have to pull out the JsonElement and then use it. The problem is JsonElement "service" and "op" are String

I would like the JsonObject to be converted like this

   {
      "team": "xyz",
      "patern": "abc",
      "service": {"version":0,"name":"some_service_name"},
      "op": {"version":0,"name":"some_op_name"},
      .
      .
      .
    }

I have tried new JsonParser().parse(string) and also new Gson().fromJson(string, JsonObject.class) but it doesn't resolve. Also tried Jolt but it parses it as String.

I know that this can be solved by mapping it to a java class and then use it but I would like to know if there is a way to get away without additional java class.

5
  • 1
    What exactly is your question? What do you expect from us? You need to parse the service and op strings, just like you parsed your outer JSON string. How you do that doesn't matter. Or you need to step back and wonder why you have jsn strings as values of a json document, and redesign the system. Commented Dec 29, 2017 at 8:30
  • use GSON conversion for this. Commented Dec 29, 2017 at 8:31
  • have a look at jackson Commented Dec 29, 2017 at 8:32
  • @JBNizet - the end system from where I'm consuming cannot be changed, they have json strings as values in json document. I have to do the processing to get the required fields. I need help with parsing those json strings or convert the whole json string to standard json (without json strings) Commented Dec 29, 2017 at 18:00
  • @SagarNayak - I know I can add a java class and map all the fields and that solution works, but I wanted more easy solution where I need not have to create additional classes. Commented Dec 29, 2017 at 18:01

3 Answers 3

2

I have not found a way to do this. Using Gson though works quite well. I would do something like this.

Gson gson = new Gson();
Map res = gson.fromJson(string, Map.class);
Map service = gson.fromJson((String)res.get("service"), Map.class);
Map op = gson.fromJson((String)res.get("op"), Map.class);
res.put("service", service);
res.put("op", op);
String newString = gson.toJson(res);
Sign up to request clarification or add additional context in comments.

Comments

0

This might help you!

JSONEqualentBeanClass jSONEqualentBeanClass = null;
com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
try{
    jSONEqualentBeanClass  = mapper.readValue(yourJSONStr, JSONEqualentBeanClass.class);
}catch(Exception e){
 // 
}

2 Comments

This works, but I wanted a solution without using bean class, basically leveraging Gson or jackson libraries
0

Using org.json library:

 JSONObject jsonObj = new JSONObject("{\"team\": \"xyz\",\"patern\": \"abc\",
 \"service\": \"{\"version\":0,\"name\":\"some_service_name\"}\",
 \"op\": \"{\"version\":0,\"name\":\"some_op_name\"}\" }");

For more ways check this link

http://www.java67.com/2016/10/3-ways-to-convert-string-to-json-object-in-java.html

1 Comment

This wont work. I need help with json strings as values in json document and parsing those json strings or convert the whole json string to standard json (without json strings), again I know this can be done by mapping it to java class but I need an alternative.

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.