Mirth Community

Mirth Community (http://www.mirthcorp.com/community/forums/index.php)
-   Support (http://www.mirthcorp.com/community/forums/forumdisplay.php?f=6)
-   -   JSON Looping question (http://www.mirthcorp.com/community/forums/showthread.php?t=219154)

tdickerson 05-21-2019 06:57 AM

JSON Looping question
 
Hi - I have a question. I am pulling information from a database and sending out a JSON message.

The template looks like this:
"geneList": [{
"gene": ""
},
{
"gene": ""
},
{
"gene": ""
}
],

My code is this:
var geneListQuery = executeCachedQuery(dbConn, geneQuery);

var geneCount = 0;

while(geneListQuery.next()){

tmp['testDetails']['geneList'][geneCount]['gene'] = geneListQuery.getString('hgnc_symbol');

}
geneCount++;

The result I am getting is this:
"geneList" : [
{
"gene" : "ZRSR2"
},
{
"gene" : ""
},
{
"gene" : ""
}
],

There are approximately 600 genes to be listed, and it is only showing the last one. Can anyone help me on this??

cory_cole 05-21-2019 09:29 AM

make this...

tmp['testDetails']['geneList'][geneCount]['gene'] = geneListQuery.getString('hgnc_symbol');

into this

tmp['testDetails']['geneList'][geneCount]['gene'] += geneListQuery.getString('hgnc_symbol');

tdickerson 05-21-2019 11:03 AM

thanks for the quick reply. I made the change, and the file looks like this:
"geneList" : [
{
"gene" : ""
},
{
"gene" : "ABI1ABL1ABL2ACKR3ACSL3ACSL6A

I was hoping for this:
"geneList": [
{
"gene": "ABI1"
},
{
"gene": "ABL1"
},
{
"gene": "ABL2"
},
{
"gene": "ACKR3"

agermano 05-21-2019 12:53 PM

Your first issue is that your counter needs to be incremented inside the while loop instead of after if you're using an index (which isn't necessary, see below.)

The += operator appends to an XMLList, but doesn't work with javascript arrays.

You have to create a new object each time.

I would change your template to an empty array:

Code:

"geneList": [],
And then try this:

Code:

var geneListQuery = executeCachedQuery(dbConn, geneQuery);

while(geneListQuery.next()){
    tmp['testDetails']['geneList'].push({
        gene: geneListQuery.getString('hgnc_symbol')
    });
}


tdickerson 05-21-2019 01:27 PM

that worked perfectly!! thanks so much!!


All times are GMT -8. The time now is 06:25 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Mirth Corporation