javajsongsonjsonforms

Issue reading Json format with Gson Java


I need to turn a full string into Json format, the challenge is that within the payload there is a nested field which requires be read as Json likewise.

My java code look like:

    String payload
    ="{\"appId\":\"APP02\",\"employeeId\":\"789-33-3887\",\"name\":\"Paolo Ledner\",\"phonenumber\":\"757.910.0396\",\"beer\":\"Sierra Nevada Bigfoot Barleywine Style Ale\",\"company\":\"Schmidt LLC\",\"address\":\"{\\\"street\\\":\\\"Coralie Trafficway\\\",\\\"zipcode\\\":\\\"12291\\\"}\"}";
Gson g = new Gson();
JsonElement jelem = g.fromJson(payload,JsonElement.class);
JsonObject jobj = jelem.getAsJsonObject();

This return properly the Json object but I can't read the keys from the nested field address, then I need to read the content of this field , turn it into string and according to my logic then apply the same approach to get the Json format and read the keys I need, here is this step:

String address = jobj.get("address").toString().substring(1,jobj.get("address").toString().length()-1);
JsonElement jeaddress = g.fromJson(address, JsonElement.class);
JsonObject jaddress = jeaddress.getAsJsonObject();

System.out.println(jaddress.get("zipcode"));

Issues:

The string address returns properly {\"street\":\"Coralie Trafficway\",\"zipcode\":\"12291\"} but then when I try to print jaddress.zipcode I get the error:

Exception in thread "main" com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected name at line 1 column 2 path $. at com.google.gson.Gson.fromJson(Gson.java:902) at com.google.gson.Gson.fromJson(Gson.java:852) at com.google.gson.Gson.fromJson(Gson.java:801) at com.google.gson.Gson.fromJson(Gson.java:773) at bncingestion.kafkaconsumer.validating_payload(kafkaconsumer.java:29) at bncingestion.kafkaconsumer.main(kafkaconsumer.java:15) Caused by: com.google.gson.stream.MalformedJsonException: Expected name at line 1 column 2 path $. at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1559) at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:513) at com.google.gson.stream.JsonReader.hasNext(JsonReader.java:414) at com.google.gson.internal.bind.TypeAdapters$29.read(TypeAdapters.java:738) at com.google.gson.internal.bind.TypeAdapters$29.read(TypeAdapters.java:714) at com.google.gson.internal.bind.TypeAdapters$35$1.read(TypeAdapters.java:910) at com.google.gson.Gson.fromJson(Gson.java:887) ... 5 more

An additional version of this transformation wihtout remove the " is here :

String address = jobj.get("address").toString();
JsonElement jeaddress = g.fromJson(address, JsonElement.class);
JsonObject jaddress = jeaddress.getAsJsonObject();
System.out.println(jaddress.get("zipcode"));

In this case the error is:

Exception in thread "main" java.lang.IllegalStateException: Not a JSON Object: "{\"street\":\"Coralie Trafficway\",\"zipcode\":\"12291\"}" at com.google.gson.JsonElement.getAsJsonObject(JsonElement.java:90) at bncingestion.kafkaconsumer.validating_payload(kafkaconsumer.java:31) at bncingestion.kafkaconsumer.main(kafkaconsumer.java:15)

I appreciate any help. thanks


Solution

  • Calling jobj.get("address").toString() formats the value (a JsonElement) as JSON, and since the value is a String, it adds quotes and escapes the content. Don't do that, i.e. don't call toString(), explicitly or implicitly, if you're after the value.

    Replace line with String address = jobj.get("address").getAsString();
    Also replace line with System.out.println(jaddress.get("zipcode").getAsString());