Paul Baumgarten

Computer Scientist, teacher, freelance programmer and self confessed geek

JSON as Data (Java)

There are a lot of competing Java JSON libraries around. I've used org.json which is available at:

When searching for tutorials and documentation, be careful to search for "parsing" JSON. You will find a lot of Java tools that refer to serialising/deserialsing objects, or that make reference to pojo (plain-old-java-objects). For simple parsing of data received from a web api, these will be more complex than your needs.


  • JavaDocs:

Sample code

package com.pbaumgarten.beyondbasics;

import org.json.*;  // JSON parsing

class JSONDemo {

    public boolean saveData(String filename) {
        // Convert the ArrayList of Person objects into a JSONArray of JSONObjects
        JSONArray ja = new JSONArray();
        for (Person p : people) {
            JSONObject jo = new JSONObject(p);
        // Save the JSONArray to a text file
        try {
            FileWriter fw = new FileWriter(filename);
            PrintWriter output = new PrintWriter(fw);
            output.println( ja.toString() );
            return true;
        } catch(Exception e) {
            System.out.println("[saveData] error saving "+filename);
            return false;

    public ArrayList<Person> loadData(String filename) {
        StringBuilder sb = new StringBuilder();
        ArrayList<Person> results = new ArrayList<Person>();
        try {
            // Load the JSON file as String data
            File f = new File(filename);
            Scanner reader = new Scanner(f);
            while (reader.hasNextLine()) {
            // Create a JSONArray from the String
            JSONArray ja = new JSONArray(sb.toString());
            // Iterate over the JSONObjects within the JSONArray
            for (int i=0; i< ja.length(); i++) {
                JSONObject o = ja.getJSONObject(i);
                // Use each JSONObject to create a Person object
                Person p = new Person(
                // Add the Person object to the ArrayList of people
            return results;
        } catch(Exception e) {
            System.out.println("[loadData] error loading "+filename);
            return null;

    public static void main(String[] args) {
        // TO DO