0

My JSON Object has some empty values ("") So While converting it throws error.

Here's my JSON

{
    "Animals": [
        {
            "id": 6140,
            "Name": ""
        },
        {
            "id": 6144,
            "Name": "Lion"
        }
    ]
}

My Code:

JsonParser parser=new JsonParser();
JsonArray myarray=parser.parse(json).getAsJsonArray();

I'm getting the error as

com.google.gson.stream.malformedjsonexception

How do I convert the empty string values into null while parsing the JSON object as a JSONArray?

I want the final result to be

[{id=6140, Name=null},{id=6144, Name="Lion"}] 
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Jayendran
  • 9,638
  • 8
  • 60
  • 103
  • So, which do you want? To convert the JSON correctly, or parse the empty string as null? Keeping in mind that an empty string is perfectly acceptable JSON. If you need it to be null, why does it have an empty string to begin with? – OneCricketeer Jan 28 '18 at 04:20
  • @cricket_007 : My use-case is just want to store the JSON array in my backend. So when I tried with using the JSONArray the conversion gets an error due to the empty string (""). The empty string doesn't work in my case.Could you explain how the empty string("") is acceptable by JSON? – Jayendran Jan 28 '18 at 04:27
  • JSON (and your backend probably) happily accept both null and strings of zero length... Your error is clearly because you trying to parse an object as an array. Nothing to do with the string – OneCricketeer Jan 28 '18 at 04:54
  • Yes when i accept the Json as a string, I can able to push the json as a plain string object(both "" and empty). But the resultant values will be like **[{id=6140.0, value=}, {id=6144.0, value=Lion}]** . Here the value of id 6140 iis difficult to process. – Jayendran Jan 28 '18 at 05:02
  • I don't know what database system you're using, but it shouldn't be difficult to process an empty string. There's little reason to make it null when an empty value is accepted in JSON, but so is null as a value – OneCricketeer Jan 28 '18 at 05:15
  • It doesn't matter the database system.Before storing the data's into the system my Value is looks like **[{id=6140.0, value=}, {id=6144.0, value=Lion}]**. In such case i want to get the value as **[{id=6144.0, value=Lion}]** – Jayendran Jan 28 '18 at 05:54
  • Wait... Your question says you want the name to be null. Now, you want to remove the element? Also, how did Name become value? – OneCricketeer Jan 28 '18 at 15:26
  • Sorry I've wrongly pasted the values. The final result will be looks like **[{id=6140.0, Name=null},{id=6144.0, Name=Lion}]** – Jayendran Jan 29 '18 at 04:57
  • Okay, fine. Still not clear where the empty string came from to begin with, or why the given answer does not suit your needs because, again, empty string is valid input – OneCricketeer Jan 29 '18 at 06:56

1 Answers1

-2

This is happening because the json you provided is not a json array, it is a json Object. you should do

    JsonParser json = new JsonParser();
    JsonObject myJsonObject = json.parse(json).getAsJsonObject();
    JsonArray jsonArray = myJsonObject.getAsJsonArray("Animals");
best wishes
  • 5,789
  • 1
  • 34
  • 59
  • 1
    This still doesn't convert the empty string into null – OneCricketeer Jan 28 '18 at 04:19
  • IMO empty string should be a separate use case all together, it has nothing to do with gson. His problem was not being able to convert to json array, and the premise was empty string is causing the issue. – best wishes Jan 28 '18 at 04:21
  • @maneesh I think cricket_007 was right. This doesn't solve my issue ! – Jayendran Jan 28 '18 at 04:29
  • 1
    @JayendranRosh once you have parsed the json, you can check if any field is null or you can write custom serializer, Please have a look https://stackoverflow.com/q/18491733/3892213 – best wishes Jan 28 '18 at 04:38
  • @maneesh Could you explain this too with my use case? With example – Jayendran Jan 28 '18 at 06:00