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.

KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
Sandeep Raikar
  • 491
  • 1
  • 6
  • 13
  • 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. – JB Nizet Dec 29 '17 at 08:30
  • use GSON conversion for this. – Sagar Nayak Dec 29 '17 at 08:31
  • have a look at `jackson` – Lino Dec 29 '17 at 08: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) – Sandeep Raikar Dec 29 '17 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. – Sandeep Raikar Dec 29 '17 at 18:01

3 Answers3

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);
leonardseymore
  • 533
  • 5
  • 20
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){
 // 
}
Abi Divi
  • 11
  • 4
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

Sreejesh K Nair
  • 563
  • 1
  • 6
  • 16
  • 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. – Sandeep Raikar Dec 29 '17 at 18:04