web stats
JSON Looping question - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 05-21-2019, 06:57 AM
tdickerson tdickerson is offline
Mirth Newb
 
Join Date: Aug 2016
Posts: 6
tdickerson is on a distinguished road
Default 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??
Reply With Quote
  #2  
Old 05-21-2019, 09:29 AM
cory_cole cory_cole is offline
Mirth Guru
 
Join Date: Mar 2012
Posts: 1,228
cory_cole is on a distinguished road
Default

make this...

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

into this

tmp['testDetails']['geneList'][geneCount]['gene'] += geneListQuery.getString('hgnc_symbol');
Reply With Quote
  #3  
Old 05-21-2019, 11:03 AM
tdickerson tdickerson is offline
Mirth Newb
 
Join Date: Aug 2016
Posts: 6
tdickerson is on a distinguished road
Default

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"
Reply With Quote
  #4  
Old 05-21-2019, 12:53 PM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 806
agermano is on a distinguished road
Default

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')
    });
}
Reply With Quote
  #5  
Old 05-21-2019, 01:27 PM
tdickerson tdickerson is offline
Mirth Newb
 
Join Date: Aug 2016
Posts: 6
tdickerson is on a distinguished road
Default

that worked perfectly!! thanks so much!!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 01:38 PM.


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