0

I am currently parsing a json file and appending the data to an HTML table. I need to create a few variables that equal 3 of the properties within my JSON object. And I need to include those variables in a function that I am appending to one of the td elements within the table for each of my JSON objects.

Here is my function to append the data...

function createPatientTable(json) {
$.each(json.LIST, function(i, COPD_QUAL) {
$('.footable > tbody:last').append('<tr><td>' + COPD_QUAL.PATIENT + '</td><td><a href="javascript:APPLINK(0,\'powerchart.exe\',\'/PERSONID='+COPD_QUAL.PERSON_ID+' /ENCNTRID='+COPD_QUAL.ENCNTR_ID+'\')">' + COPD_QUAL.FIN + '</a></td><td>' + COPD_QUAL.NURSE_UNIT + '</td><td>' + COPD_QUAL.ROOM +   '</td><td>' + COPD_QUAL.BED +'</td><td>' + COPD_QUAL.ATTENDING_PHYS + '</td><td>' +  COPD_QUAL.LENGTH_OF_STAY + '</td><td class="assessment ' + getSeverity(COPD_QUAL.MED_ASSESS)   + '" onclick="openPowerform">' + COPD_QUAL.MED_ASSESS + '</td></tr>');
});
$('.footable').footable();
};

Here is one of my JSON objects (formatted for readability):

{
     "COPD_QUAL":15, 
     "LIST":[ 
        {
            "PATIENT": "TEST,    TRICKLE",
            "FIN": "70100905",
            "NURSE_UNIT": "TIC",
            "ROOM": "C219",
            "BED": "A",
            "ATTENDING_PHYS": "LEVITEN , DANIEL L",
            "LENGTH_OF_STAY": "171days 02:14:15",
            "MED_ASSESS": "Mild exacerbation",
            "ACTIVITY_ID": "305675472.0000",
            "PERSON_ID": 8986122.000000,
            "ENCNTR_ID": 14150574.000000
         }
       ]
     }

I need to plug COPD_QUAL.PERSON_ID, COPD_QUAL.ENCNTR_ID, and COPD_QUAL.ACTIVITY_ID into my below function so when the td element is clicked, the below function triggers with the personid, encntrid, and activityid of the JSON object that has been appended to that row:

function openPowerform() {
   var dPersonId = "COPD_QUAL.PERSON_ID";
   var dEncounterId = "COPD_QUAL.ENCNTR_ID";
   var formId = 0.0;
   var activityId = "COPD_QUAL.ACTIVITY_ID";
   var chartMode = 1;
   var mpObj = window.external.DiscernObjectFactory("POWERFORM");
   mpObj.OpenForm(dPersonId, dEncounterId, formId, activityId, chartMode);
};

How can I successfully make these variables "dynamically" equal my JSON values? (since the values are different per object/string within my JSON file).

Thanks in advance!

gwells2
  • 5
  • 4
  • use [JSON.parse](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) – Matt Burland Apr 04 '14 at 20:21
  • possible duplicate of [How to parse JSON in JavaScript](http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript) – zero298 Apr 04 '14 at 20:23
  • 3
    `COPD_QUAL` is `15`, not a sub-object. `PERSON_ID` is a property of the object that's an element of the `LIST` array. You need to clarify what you're really trying to reach. – Barmar Apr 04 '14 at 20:24

1 Answers1

0

Your JSON is mal-formed. Here is a valid object:

{
    "COPD_QUAL": 15,
    "LIST": [
        {
            "PATIENT": "TEST,    TRICKLE",
            "FIN": "70100905",
            "NURSE_UNIT": "TIC",
            "ROOM": "C219",
            "BED": "A",
            "ATTENDING_PHYS": "LEVITEN , DANIEL L",
            "LENGTH_OF_STAY": "171days 02:14:15",
            "MED_ASSESS": "Mild exacerbation",
            "ACTIVITY_ID": "305675472.0000",
            "PERSON_ID": 8986122,
            "ENCNTR_ID": 14150574
        }
    ]
}

Each object within the LIST array has to be addressed by its array position. With this object, your object property references would be:

LIST[0].PERSON_ID, LIST[0].ENCNTR_ID, and LIST[0].ACTIVITY_ID

I don't know if this is how you want to structure your JSON in the end, but this is what you have now. Also, there is no absolute need to run this through JSON.parse(). It can be treated like a JavaScript Object Literal and accessed the same way.

MBielski
  • 6,628
  • 3
  • 32
  • 43