Announcement

Collapse
No announcement yet.

JSON Looping question

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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??

  • #2
    make this...

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

    into this

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

    Comment


    • #3
      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"

      Comment


      • #4
        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')
            });
        }

        Comment


        • #5
          that worked perfectly!! thanks so much!!

          Comment

          Working...
          X