I am trying to get a user’s input information, have the program save the information to a JSON file, and then be able to input more information in a different run without overwriting the entire file. The idea is that it will work like an account database.
While my code involves much more than this snippet, this is essentially what I need. Also, I am using IntelliJ IDEA as my IDE.
public static void accountSetup() throws IOException {
// Variables
File accounts = new File("accounts.json");
ObjectMapper mapper = new ObjectMapper();
ArrayNode arNode = mapper.createArrayNode();
ObjectNode obNode = mapper.createObjectNode();
Scanner scan = new Scanner(System.in);
// Get user info
System.out.println("What is the account holder's name?");
obNode.put("name",scan.nextLine());
System.out.println("Please enter a 7-digit passcode");
obNode.put("passcode",scan.nextInt());
// Write to JSON file
mapper.enable(SerializationFeature.INDENT_OUTPUT);
arNode.add(obNode);
mapper.writeValue(accounts,arNode);
}
Here’s what I’m expecting from this (^) snippet, assuming the user put in “Sam” and “1234567”, and then re-ran the program and input “Max” and “7654321”:
[
{
"name" : "Sam",
"passcode" : 1234567
},
{
"name" : "Max",
"passcode" : 7654321
}
]
However, when I try to run the program again and input different information, it completely overwrites the file, leaving me with this:
[
{
"name" : "Max",
"passcode" : 7654321
}
]
I understand that this has something to do with how the ObjectMapper writes to the file, and I’ve done quite a bit of digging, but to no avail.
I would like to continue to use Jackson as it seems to be a powerful and reliable tool when it comes to JSON, and I started the project this was for as a means to explore different aspects of Java. In other words, I would like to leave switching libraries as a last resort.
Please keep in mind that I am barely a novice programmer at best, and some things that might seem stupid or obvious aren’t to me.
TL;DR I am essentially trying to append an ObjectNode to a JSON array between runtimes… and can’t figure it out. Any assistance would be greatly appreciated. Thank you for your time.
SourMnMs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.