Adding elements to an JSON object in a external JSON file?
Question: * * *

(After months of surfing the internet, talking to the school's computing
department and try code out, I still don't get how to do it, but I do know
more specific about what I trying to do)

* * *

Previously I said I want to "Add lines" to a existing JSON file.

What I want to do is simply add an element to an JSON object from a file, then
save the file. However I am still confused about how to do it.

The process I am guessing is to use ajax to load the content of the file (the
JSON code in the file) into a variable then add the new element into the
object then save the file.

I have seen a lot of code but are all just too confusing and looks like its
for webpages. I am trying to edit a file on the computer as a program which I
think webpage related code such as xmlhttp requests are irrelevant as the file
is in a folder in appdata.

I have been confused and thought Java and Javascript were the same thing, I
know now they're not.

What code or functions would I look for and how would it be used in the code?

(Please don't post pseudocode because I have no idea how to write the code for
them since I have literally no idea how to code anything other than a html
webpage and some php. Other coding language like Java, Javascript and Python I
have little knowledge with but not enough to write a program alone.)


Answer: I think it would be best to use code that somebody else has already written to
manipulate the JSON. There are plenty of libraries for that, and the best
would be the officially specified one, [JSON-P](https://jsonp.java.net). What
you would do is this:

  1. Go to <http://jsonp.java.net/> and download JSON-P. (You will have to examine the page carefully to find the link to "[JSON Processing RI jar](http://search.maven.org/remotecontent?filepath=org/glassfish/javax.json/1.0.4/javax.json-1.0.4.jar)".) You will need to include this JAR in your class path while you write your program. 

  2. Add imports to your program for `javax.json.*`.

  3. Write this code to do the job (you will have to catch `JsonException`s and `IOException`s):
    
        JsonReader reader = Json.createReader(new FileReader("launcher_profiles.json"));
    JsonObject file = reader.readObject();
    reader.close();
    JsonObject profiles = file.getJsonObject("profiles");
    JsonObject newProfile = Json.createObjectBuilder()
                                .add("name", "New Lines")
                                .add("gameDir", "New Lines")
                                .add("lastVersionId", "New Lines")
                                .add("playerUUID", "")
                            .build();
    JsonObjectBuilder objectBuilder = Json.createObjectBuilder()
                                        .add("New Profile Name", newProfile);
    for (java.util.Map.Entry<String, JsonValue> entry : profiles.entrySet())
        objectBuilder.add(entry.getKey(), entry.getValue());
    JsonObject newProfiles = objectBuilder.build();
    
    // Now, figure out what I have done so far and write the rest of the code yourself! At the end, use this code to write out the new file:
    JsonWriter writer = Json.createWriter(new FileWriter("launcher_profiles.json"));
    writer.writeObject(newFile);
    writer.close();
    

