I am currently working on migrating a database from a non-SQL source to an SQL database. The non-SQL source outputs the data in a JSON doc that is just a series of independent JSON objects. I an using JSONObject within Java, and that (to my understanding) can only recognize the top most object within the document. In order to get around this issue I am writing code to convert the independent objects into an array.
The current method I am using involves converting the JSON doc into a string, counting curly brackets to find objects, and then inserting them into an array.
for (int i = 0; i < doc.length(); i++) {
char currentChar = doc.charAt(i);
if (currentChar == '{') {
Integer jsonStart = i;
Integer openBrace = 1;
Integer closeBrace = 0;
while (openBrace > closeBrace) {
i++;
currentChar = doc.charAt(i);
if (currentChar == '{') {
openBrace++;
}
if (currentChar == '}') {
closeBrace++;
}
}
Integer jsonEnd = i;
String currentString = doc.substring(jsonStart, jsonEnd + 1);
JSONObject currentJSONObject = new JSONObject(currentString);
returnJSONArray.put(currentJSONObject);
Due to size, the database had to be divided into multiple 10k object documents. The code worked well until one of the documents had braces stored within the value. So I added some code to watch for values and ignore those based on quotation marks beneath the close curly bracket counter.
if (currentChar == '"') {
i++;
currentChar = mongoExport.charAt(i);
while (!(currentChar == '"')) {
i++;
currentChar = mongoExport.charAt(i);
}
This worked for the document with value pairs that contained curly brackets, but upon testing it against the rest of the documents I experience a "String index out of range: big number" error in one of the other documents that traces back to the while loop looking for and end quotation mark. From what I can figure, this means that there are also values that contain quotation marks. I tried some code to check for escape characters before quotation marks, but that changed nothing. I can't check through these documents manually, they are far too long for that. Is there a way for me to handle these strings? Also, was there a far easier method I could have used that I was unaware of from the beginning?