Search code examples
javajsonjackson

Trouble writing data to JSON file without overwriting past data across multiple runs (using Jackson)


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.


Solution

  • This is my first time using Jackson, so there may be a better way. I also see that you are using deprecated methods. That's not a good thing.

    Try doing things in order.

    1. Read the file to a string.
    2. Read the string to a JsonNode.
    3. Write the JsonNode to an output JsonArray
    4. Write the data from the user input to the same output JsonArray.
    5. Write the output JsonArray to File and overwrite the old file
    import com.fasterxml.jackson.databind.JsonNode;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.SerializationFeature;
    import com.fasterxml.jackson.databind.node.ArrayNode;
    import com.fasterxml.jackson.databind.node.ObjectNode;
    import java.io.File;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.util.Scanner;
    
    /**
     *
     * @author Sed
     */
    public class JavaTestingTwo {
    
        public static void main(String[] args) 
        {
            try 
            {           
                File accounts = new File("accounts.json");
                String jsonString = Files.readString(accounts.toPath());
                
                
                
                ObjectMapper objectMapper = new ObjectMapper();  
                ArrayNode arNode = objectMapper.createArrayNode();
                
                JsonNode arrayNode = objectMapper.readTree(jsonString);
                if (arrayNode.isArray()) {
                    // yes, loop the JsonNode and display one by one
                    for (JsonNode node : arrayNode) {
                        ObjectNode obNode = objectMapper.createObjectNode();
                        System.out.println("Name: " + node.get("name").asText());
                        System.out.println("Passcode: " + node.get("passcode").asInt());
                        obNode.put("name", node.get("name").asText());
                        obNode.put("passcode", node.get("passcode").asInt());
                        arNode.add(obNode);
                    }
                }
                
                // Get user info
                ObjectNode obNode = objectMapper.createObjectNode();
                Scanner scan = new Scanner(System.in);
                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());
        
                
                objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
                arNode.add(obNode);
                objectMapper.writeValue(accounts,arNode);
                
            } 
            catch (IOException ex)
            {
                System.out.println(ex.toString());
            }
        }
    }
    

    Output

    [ 
        {
            "name" : "Sam",
            "passcode" : 1234567
        }, 
        {
            "name" : "Max",
            "passcode" : 7654321
        }, 
        {
             "name" : "john",
             "passcode" : 1234567
        }
    ]