Previous Next JavaScript must be enabled to correctly display this content

## UpdateJSON.loadTable()

The `UpdateJSON.loadTable()` method loads our sample date into the Oracle NoSQL Database store.

As Sample Data shows, all of our table rows are represented as JSON data in a single text file. Each row is a single JSON object in the file. Typically JSON files are expected to contain one and only one JSON object. While there are third party libraries which will iterate over multiple JSON objects found in a stream, we do not want to rely on them for this example. (In the interest of simplicity, we avoid adding a third party dependency.) Consequently, this method provides a primitive custom parser to load the example data into the store.

        // Loads the contents of the sample data file into
        // the personContacts table. The defineTable() method
        // must have been run at least once (either in this
        // runtime, or in one before it) before this method
        // is run.
        //
        // JSON parsers ordinarily expect one JSON Object per file.
        // Our sample data contains multiple JSON Objects, each of
        // which represents a single table row. So this method
        // implements a simple, custom, not particularly robust
        // parser to read the input file, collect JSON Objects,
        // and load them into the table.
        private void loadTable(KVStore kvstore, String file2load) {
            TableAPI tableH = kvstore.getTableAPI();
            Table myTable = tableH.getTable("personContacts");

            BufferedReader br = null;
            FileReader fr = null;

            try {
              String jObj = "";
              String currLine;
              int pCount = 0;
              boolean buildObj = false;
              boolean beganParsing = false;

              fr = new FileReader(file2load);
              br = new BufferedReader(fr);

              // Parse the example data file, loading each JSON object
              // found there into the table.
              while ((currLine = br.readLine()) != null) {
                  pCount += countParens(currLine, '{');

                  // Empty line in the data file
                  if (currLine.length() == 0)
                      continue;

                  // Comments must start at column 0 in the
                  // data file.
                  if (currLine.charAt(0) == '#')
                      continue;

                  // If we've found at least one open paren, it's time to
                  // start collecting data
                  if (pCount > 0) {
                      buildObj = true;
                      beganParsing = true;
                  }

                  if (buildObj) {
                      jObj += currLine;
                  }

                  // If our open and closing parens balance (the count
                  // is zero) then we've collected an entire object
                  pCount -= countParens(currLine, '}');
                  if (pCount < 1)
                      buildObj = false;
                  // If we started parsing data, but buildObj is false
                  // then that means we've reached the end of a JSON
                  // object in the input file. So write the object
                  // to the table, which means it is written to the
                  // store.
                  if (beganParsing && !buildObj) {
                      Row row = myTable.createRowFromJson(jObj, false);
                      tableH.put(row, null, null);
                      jObj = "";
                  }

              }

              System.out.println("Loaded sample data " + file2load);

            } catch (FileNotFoundException fnfe) {
                System.out.println("File not found: " + fnfe);
                System.exit(-1);
            } catch (IOException ioe) {
                System.out.println("IOException: " + ioe);
                System.exit(-1);
            } finally {
                try {
                    if (br != null)
                        br.close();
                    if (fr != null)
                        fr.close();
                } catch (IOException iox) {
                    System.out.println("IOException on close: " + iox);
                }
            }
        }

        // Used by loadTable() to know when a JSON object
        // begins and ends in the input data file.
        private int countParens(String line, char p) {
            int c = 0;
            for( int i=0; i < line.length(); i++ ) {
                if( line.charAt(i) == p ) {
                        c++;
                }
            }

            return c;
        }