#Gson JSON parsing. If a property is missing, no error is being thrown.

20 messages · Page 1 of 1 (latest)

violet lily
#

Hi, im using gson to parse a json file but i dont know how to handle if ONE of the objects inside the array doesnt have all the properties it should. should i just do a for loop and check if any property is null or is there a better way to do it?
this is some my code:

System.out.println("What is the name of the event file?");
filename = scan.nextLine();
try {
    JsonReader reader = new JsonReader(new FileReader(filename));
    events = gson.fromJson(reader, Events.class);
    events.getEvents()[0].printEvent();
} catch (IOException ioe) {
    System.out.println("The file " + filename + " could not be found.");
} catch (JsonSyntaxException e) {
                System.out.println("The file " + filename + " is not formatted properly.");
    }
}

and this is the json

{
  "data": [
    {
      "name": "P!NK",
      "tour": "Summer Carnival 2023 Tour",
      "localDate": "2023-10-05",
      "venue": "SoFi Stadium"
    },
    {
      "name": "Eagles",
      "tour": "Hotel California 2023 Tour",
      "localDate": "2023-02-24",
      "venue": "Acrisure Arena"
    },

events is an array of the class event, which is:

public class Event {
  private String name;
  private String tour;
  private String localDate;
  private String venue;    
} // getters and printevent which basically prints all variables here

so for exmaple, if i remove venue from the first one, printEvent would print the correct info except for venue, which prints null
as i asked before, is checking if there is any null property the only way to do it or is there an easier way to immediately call the JsonSyntaxException or is there any other exception?

swift matrixBOT
#

This post has been reserved for your question.

Hey @violet lily! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

violet lily
#

also, should Events (the class that has an array that stores multiple Event instances) use a list or is normal array fine?

unique kettle
#

Why do you want an exception?
I if you just want don't to print I would check if it's not null and then print.

violet lily
#

gson.toJson(events.getEvents(), new FileWriter(filename)); this just deletes the whole json file.
String json = gson.toJson(events.getEvents()); and writer.write(json); only has the array, its missing the parent data

swift matrixBOT
#

<@&765578700724371486>

Requested by Arat#7539
violet lily
#

gson.toJson(events.getEvents(), new FileWriter(filename)); leaves the json file empty

String json = gson.toJson(events.getEvents());
FileWriter writer = new FileWriter(filename);
writer.write(json);
#
[
  {
    "name": "P!NK",
    "tour": "Summer Carnival 2023 Tour",
    "localDate": "2023-10-05",
    "venue": "SoFi Stadium"
  },
  {
    "name": "Eagles",
    "tour": "Hotel California 2023 Tour",
    "localDate": "2023-02-24",
    "venue": "Acrisure Arena"
  },
]
``` leaves the json file like this
#

as you can see above, i want the array to be a child of data

#

basically, it should look like this:

#
{
  "data": [
    {
      "name": "P!NK",
      "tour": "Summer Carnival 2023 Tour",
      "localDate": "2023-10-05",
      "venue": "SoFi Stadium"
    },
    {
      "name": "Eagles",
      "tour": "Hotel California 2023 Tour",
      "localDate": "2023-02-24",
      "venue": "Acrisure Arena"
    },
  ]
}
unique kettle
#

Cause you don't want to save the event array.
Try to save the Events class which contains the event array

unique kettle
cunning crow
#

Hi @violet lily and @unique kettle , I’m Alexander Lill (not a bot!), a researcher at the University of Zurich. I’m trying to help people receive answers faster on Discord. If you are ok with giving me some feedback, please read on, otherwise click the ⛔ symbol, and I will delete this message and apologize for the inconvenience.

Would any of the provided links have helped to answer the OPs question? Please click the emojis below each link to let me know if the link was helpful ✅ or not helpful ❌.

DM me if you have any questions. More info: #announcements message

#

➡️ Dates HostName - How to Collect HostNames in a JsonArray
<#help-barraquito message>

#

➡️ How to Parse an Array of JSON Objects
<#help-lungo message>

#

➡️ Java ArrayList Parser - How to Parse a List in JSON
<#help-frappé message>

cunning crow
#

Thank you for your feedback @unique kettle!

I’d like to invite you along anybody that is interested to fill in a short follow-up survey on your experience with the suggested links and your feedback (max. 10 mins). It would be of great help for my research (and my suggestions).

Your responses will be treated confidentially. For your efforts, you can decide to be entered in a raffle for an Amazon gift card of USD 100 value (voluntary).

To participate in the survey, please answer the questions in the following link:
https://uzhwwf.qualtrics.com/jfe/form/SV_1zWdrjjRfwFGXwa?ST=DI&CO=JC&CI=8414952&LA=1