I'm using Javascript/Java to query an MSSQL database, and then use the CachedRowSet received from the query to add some information to a Json object. Here's the code that's querying the database and adding the results to the object.
var sqlGetVariables = "SELECT MappingVariable.Id as Id, MappingVariable.Name AS Name, MappingVariable.Value AS Value FROM MappingGroup LEFT JOIN MappingVariable ON MappingGroup.Id = MappingVariable.MappingGroupId WHERE MappingGroup.Id = " + mappingGroupId + " AND MappingVariable.Id IS NOT NULL";
var resultVariables = uberfaceDB.executeCachedQuery(sqlGetVariables);
while (resultVariables.next()) {
var variableId = resultVariables.getObject("Id");
var variableName = resultVariables.getObject("Name");
var variableMapping = resultVariables.getObject("Value");
jsonArray["Segments"][seg]["Variables"][variableName] = {};
jsonArray["Segments"][seg]["Variables"][variableName]["Mapping"] = variableMapping;
}
The issue I'm having is that there's something goofy going on with the last line where I assign "variableMapping" to the Json object. The line of code executes just fine, but when I go to iterate through the Json object later, I get stuck in an infinite recursion that keeps cycling through "class" "package" and "specificationversion".
Here's the code I'm using to iterate through the object.
function echoJson (jsonArray, strStuff) {
var strJson = "" + strStuff;
for (var item in jsonArray) {
logger.error(strJson + " --> " + item);
echoJson(jsonArray[item], strJson + " --> " + item);
}
}
The logger.error function is built in to the application I'm coding in (called Mirth Connect). It simply outputs the message to a console for error/debugging purposes. Here's an example of what I would EXPECT to get with the echoJson function:
Segments --> MSH --> Segments --> PID --> Variables --> PatientFirstName --> Mapping --> variableMappingValue
But here's what I'm actually getting:
Segments --> MSH --> Segments --> PID --> Variables --> PatientFirstName --> Mapping --> class --> package --> specificationVersion --> class --> package --> specificationVersion --> class --> package --> specificationVersion...
It continues repeating the "class --> package --> specificationVersion" portion infinitely until I stop the application.
If I replace the following line of code:
jsonArray["Segments"][seg]["Variables"][variableName]["Mapping"] = variableMapping;
With a hard coded string, like
jsonArray["Segments"][seg]["Variables"][variableName]["Mapping"] = "test";
Then it works without any problems.
Thanks in advance for taking a look at this!!
I found the solution, as explained on another programmer's blog.
There was an issue with the data type being stored in the CachedRowSet. I don't know the exact data type, but from what I gathered the "variableMapping" variable is a byte array, and I'm needing to add it to my Json object as a string. I had to do "new String(variableMapping)" to convert the array into a string, and that fixed my problem.
How I found the problem: I decided to try using JSON.stringify() to output my Json object, and received the following error:
Java class "[B" has no public instance field or method named "toJSON"
Googling that led me to the blog post I linked to above.