0

I want to convert my Blank Json array to the Null Json array.

For ex., My Json array is like "[{}]" and if I got this array then automatically converts to "[]".

My code is like as below :

JsonObject jo = FetchData.getAllItemsAvg(request.getParameter("where"), request.getParameter("lastNum"),request.getParameter("limitAvgNum"));
JsonArray ja = new JsonArray();
ja.add(jo); // Some times ja like "[{}]" .
Pshemo
  • 122,468
  • 25
  • 185
  • 269
yogesh jalodara
  • 101
  • 1
  • 12
  • 3
    It's not an empty array, it is an array containing a single object with no properties. – Boris the Spider Apr 12 '16 at 12:35
  • Okay, Do you know how to convert? – yogesh jalodara Apr 12 '16 at 12:50
  • Proper answer depends on what is your input (is it string or instance of some kind of JSONArray class) and what JSON library you are using. If it is string then maybe simply `replace` each `{}` with empty string. – Pshemo Apr 12 '16 at 12:58
  • Hi, My code is like : JsonArray ja = new JsonArray(); ja.add(jo); ja.toString().replace("{}", ""); jo = Json Object. I have tried with this but still not working. Thank you. – yogesh jalodara Apr 12 '16 at 13:02
  • Please add that information to your question. – Pshemo Apr 12 '16 at 13:06
  • Hi, added. Please have a look. – yogesh jalodara Apr 12 '16 at 13:09
  • My solution was meant for input which is string (also strings are immutable so `replace` can't affect original string, instead it creates and return new one based on original string). Anyway can we assume you are using gson library? Or is it other library (we need that info to know what methods are available). – Pshemo Apr 12 '16 at 13:11

1 Answers1

2

Check if the object is empty before adding it to the array. (assuming you're using JsonObject):

JsonObject jo = FetchData.getAllItemsAvg(
                          request.getParameter("where"),
                          request.getParameter("lastNum"),
                          request.getParameter("limitAvgNum"));
JsonArray ja = new JsonArray();
if(!jo.isEmpty()){
    ja.add(jo);
}

For com.google.gson.JsonObject:

JsonObject jo = FetchData.getAllItemsAvg(
                          request.getParameter("where"),
                          request.getParameter("lastNum"),
                          request.getParameter("limitAvgNum"));
JsonArray ja = new JsonArray();
if(!jo.entrySet().isEmpty()){
    ja.add(jo);
}
marthursson
  • 3,242
  • 1
  • 18
  • 28
  • Can you tell me how to check? Because I have tried with following but i'm fail : http://stackoverflow.com/questions/19170338/how-to-test-if-json-object-is-empty-in-java http://stackoverflow.com/questions/12585492/how-to-test-if-a-jsonobject-is-null-or-doesnt-exist And so on.. – yogesh jalodara Apr 12 '16 at 13:16
  • Yea, i have already tried with this but i'm getting "The method isEmpty() is undefined for the type JsonObject" error. – yogesh jalodara Apr 12 '16 at 13:19
  • Which JsonObject are you using (link to API preferred)? – marthursson Apr 12 '16 at 13:20
  • Yea got my answer. Thank you for helping me. I have use "jo.entrySet().size() > 0" instead of "!jo.isEmpty()" – yogesh jalodara Apr 12 '16 at 13:27