title: How to extract each element (Bookmark) from a json file as an item using Java?
body: <p>I’m trying to extract my bookmarks from Chrome’s bookmarks file, which is stored in the json format.  I have a large number of bookmarks.  This sample file below is a new Google profile to make a small number of elements for a workable file.</p>
  
  <p>So far I have constructed a java program to iterate through the file and extract the keys.  My problem is that some of the keys are arrays and have multiple keys in the item.  At present I’m trying to figure out how to get those individual elements.</p>
  
  <p>Each of the bookmarks are identified with a unique ID.  So if I can get the key by ID, then associate each of the other elements of the key with that unique ID, I believe I’ll have each of the bookmarks.</p>
  
  <p>My final objective is to put the bookmarks into a database so that I can have a better way of organizing them such as, searching, finding duplicates, categorizing and adding comments, etc.</p>
  
  <p>My java program is the below.  Also below the java program is the output after running it on the attached Chrome bookmark file.</p>
  
  <p>Bookmarks file:<br>
  <strong>Bookmarks.json:</strong></p>
  
  <pre><code>{
     "checksum": "d27be6b28b9a8879c2cb9ba6fc90df21",
     "roots": {
        "bookmark_bar": {
           "children": [ {
              "date_added": "13081990058553125",
              "id": "7",
              "meta_info": {
                 "stars.id": "ssc_c257c6390425956c",
                 "stars.version": "sync.server.Chrome45"
              },
              "name": "Google",
              "sync_transaction_version": "1",
              "type": "url",
              "url": "https://www.google.com/"
           }, {
              "date_added": "13078166246742000",
              "id": "9",
              "meta_info": {
                 "stars.flags": "5",
                 "stars.id": "ssc_7150b291c6b52a37",
                 "stars.pageData": "Ig5keGVLcUJvcW5kTjZSTQ==",
                 "stars.type": "2"
              },
              "name": "Apollo III Communications",
              "sync_transaction_version": "1",
              "type": "url",
              "url": "http://www.apollo3.com/"
           } ],
           "date_added": "13113606994595146",
           "date_modified": "13083379523340359",
           "id": "1",
           "name": "Bookmarks bar",
           "type": "folder"
        },
        "other": {
           "children": [  ],
           "date_added": "13113606994595154",
           "date_modified": "0",
           "id": "2",
           "name": "Other bookmarks",
           "type": "folder"
        },
        "sync_transaction_version": "5",
        "synced": {
           "children": [  ],
           "date_added": "13113606994595157",
           "date_modified": "0",
           "id": "3",
           "name": "Mobile bookmarks",
           "type": "folder"
        }
     },
     "version": 1
  }
  </code></pre>
  
  <p>Java Program to iterate through and extract the bookmarks:<br>
  <strong>getChromeBookmarks.java</strong></p>
  
  <pre><code>import java.io.File;
  import java.io.FileNotFoundException;
  import java.util.Scanner;
  
  import org.json.simple.JSONObject;
  import org.json.simple.parser.JSONParser;
  import org.json.simple.parser.ParseException;
  
  public class getChromeBookmarks {
  
      @SuppressWarnings("resource")
      public static void main(String[] args) {
  
          String infile = "/home/users/l/j/ljames/work/json/Bookmarks.json";
  
          String content = null;
          try {
              content = new Scanner(new File(infile)).useDelimiter("\\Z").next();         
          } catch (FileNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
          JSONParser parser = new JSONParser();
          try {
              JSONObject json = (JSONObject) parser.parse(content);
              printJsonObject(json);
  
          } catch (ParseException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
      }
  
      public static void printJsonObject(JSONObject jsonObj) {
          for (Object key : jsonObj.keySet()) {
              // based on the key types
              String keyStr = (String) key;
              Object keyvalue = jsonObj.get(keyStr);
  
              // Print key and value
              System.out.println("key: " + keyStr + " value: " + keyvalue);
  
              // expand(keyvalue);
  
              // for nested objects iteration if required
              if (keyvalue instanceof JSONObject)
                  printJsonObject((JSONObject) keyvalue);
          }
      }
  }
  </code></pre>
  
  <p><strong>Output of the java/jason program:</strong></p>
  
  <pre><code>key: checksum value: d27be6b28b9a8879c2cb9ba6fc90df21
  key: roots value: {"other":{"date_added":"13113606994595154","date_modified":"0","children":[],"name":"Other bookmarks","id":"2","type":"folder"},"synced":{"date_added":"13113606994595157","date_modified":"0","children":[],"name":"Mobile bookmarks","id":"3","type":"folder"},"bookmark_bar":{"date_added":"13113606994595146","date_modified":"13083379523340359","children":[{"date_added":"13081990058553125","meta_info":{"stars.id":"ssc_c257c6390425956c","stars.version":"sync.server.Chrome45"},"name":"Google","id":"7","type":"url","url":"https:\/\/www.google.com\/","sync_transaction_version":"1"},{"date_added":"13078166246742000","meta_info":{"stars.pageData":"Ig5keGVLcUJvcW5kTjZSTQ==","stars.id":"ssc_7150b291c6b52a37","stars.type":"2","stars.flags":"5"},"name":"Apollo III Communications","id":"9","type":"url","url":"http:\/\/www.apollo3.com\/","sync_transaction_version":"1"}],"name":"Bookmarks bar","id":"1","type":"folder"},"sync_transaction_version":"5"}
  key: other value: {"date_added":"13113606994595154","date_modified":"0","children":[],"name":"Other bookmarks","id":"2","type":"folder"}
  key: date_added value: 13113606994595154
  key: date_modified value: 0
  key: children value: []
  key: name value: Other bookmarks
  key: id value: 2
  key: type value: folder
  key: synced value: {"date_added":"13113606994595157","date_modified":"0","children":[],"name":"Mobile bookmarks","id":"3","type":"folder"}
  key: date_added value: 13113606994595157
  key: date_modified value: 0
  key: children value: []
  key: name value: Mobile bookmarks
  key: id value: 3
  key: type value: folder
  key: bookmark_bar value: {"date_added":"13113606994595146","date_modified":"13083379523340359","children":[{"date_added":"13081990058553125","meta_info":{"stars.id":"ssc_c257c6390425956c","stars.version":"sync.server.Chrome45"},"name":"Google","id":"7","type":"url","url":"https:\/\/www.google.com\/","sync_transaction_version":"1"},{"date_added":"13078166246742000","meta_info":{"stars.pageData":"Ig5keGVLcUJvcW5kTjZSTQ==","stars.id":"ssc_7150b291c6b52a37","stars.type":"2","stars.flags":"5"},"name":"Apollo III Communications","id":"9","type":"url","url":"http:\/\/www.apollo3.com\/","sync_transaction_version":"1"}],"name":"Bookmarks bar","id":"1","type":"folder"}
  key: date_added value: 13113606994595146
  key: date_modified value: 13083379523340359
  key: children value: [{"date_added":"13081990058553125","meta_info":{"stars.id":"ssc_c257c6390425956c","stars.version":"sync.server.Chrome45"},"name":"Google","id":"7","type":"url","url":"https:\/\/www.google.com\/","sync_transaction_version":"1"},{"date_added":"13078166246742000","meta_info":{"stars.pageData":"Ig5keGVLcUJvcW5kTjZSTQ==","stars.id":"ssc_7150b291c6b52a37","stars.type":"2","stars.flags":"5"},"name":"Apollo III Communications","id":"9","type":"url","url":"http:\/\/www.apollo3.com\/","sync_transaction_version":"1"}]
  key: name value: Bookmarks bar
  key: id value: 1
  key: type value: folder
  key: sync_transaction_version value: 5
  key: version value: 1
  </code></pre>
  
  <p><strong>Update</strong>: This is an example of what I am trying to do, code from:<br>
  <a href="https://stackoverflow.com/a/40887240/1204365">https://stackoverflow.com/a/40887240/1204365</a></p>
  
  <pre class="lang-java prettyprint-override"><code>import java.io.FileReader;
  
  import org.json.simple.JSONArray;
  import org.json.simple.JSONObject;
  import org.json.simple.parser.JSONParser;
  
  public class Bookmark {
  
      private static String jsonFile = "/home/users/l/j/ljames/.config/google-chrome/Default/Bookmarks";
  
      public static void main(String[] args) {
          // TODO Auto-generated method stub
  
          FileReader reader = new FileReader(jsonFile); // access the file
  
          JSONObject jsonObject = (JSONObject) new JSONParser().parse(reader);
          String checksum = jsonObject.optString("checksum");
  
          // get root object
          JSONObject root = jsonObject.getJSONObject("roots");
  
          // get root bookmarks object from root
          JSONObject bookmarks = root.getJSONObject("bookmark_bar");
  
          // get root children array from bookmarks
          JSONArray childrens = bookmarks.getJSONArray("children");
  
          JSONObject temp;
          for (int i = 0; i &lt; childrens.size(); i++) {
              // get object using index from childrens array
              temp = childrens.getJSONObject(i);
  
              // get url
              String url = temp.optString("url");
          }
  
      }
  
  }
  </code></pre>
  
  <p>The output/errors are:</p>
  
  <pre><code>check: b8b257094128d165d7ccc70d0498cc87
  Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to org.json.simple.JSONArray
      at javaTools.JsonParser.main(JsonParser.java:27)
  </code></pre>
  
  <p>There are six red marks in Eclipse.  They are on the lines below.  The suggested fix for each line is the same as the first... (highlight in bold text):</p>
  
  <pre>
  Line 19: String checksum = jsonObject.optString("checksum");
  
  Error:   Suggesting:
  
  <b>Change to 'toJSONString(..)'
  Add cast to 'temp'
  Rename in file (Ctrl+2 R)</b>
  
  This same suggestion is repleated for the other five red error marks.
  
  Line 22: JSONObject root = jsonObject.getJSONObject("roots");
  
  Line 25: JSONObject bookmarks = root.getJSONObject("bookmark_bar");
  
  Line 28: JSONArray childrens = bookmarks.getJSONArray("children");
  
  Line 33: temp = childrens.getJSONObject(i);
  
  Line 36: String url = temp.optString("url");
  </pre>