Question

forgive me for the newbie question... I'm sure this has a pretty straightforward solution. i just could not find anything that concisely explained it to me online. I have a JSON as such:

  var POSTOBJ = {
   "Subject" : {
       "BiographicData" : [
           {
               "Key" : "BarcodeID",
               "Value" : "567891234"
           },
           {
               "Key" : "Gender",
               "Value" : "Male"
           },
           {
               "Key" : "BirthDate",
               "Value" : "8/20/1964"
           },
           {
               "Key" : "Name",
               "Value" : "Success"
           }
       ]
   },
   "GroupID" : "84",
   "ClientID" : "8"
}

All I want do do is add another Key/Value pair but instead of hardcoding the value I need to put a variable instead. So I have var val = hex2a(....) where val stores the output of a barcode scan. All I want to do here is put that value into my JSON. Something to this effect:

var POSTOBJ = {
   "Subject" : {
       "BiographicData" : [
           {
               "Key" : "BarcodeID",
               "Value" : "567891234"
           },
           //WHAT I AM TRYING TO ACCOMPLISH BELOW
           {
               "Key" : "BarcodePayload",
               "Value" : val;
           },
           //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
           {
               "Key" : "Gender",
               "Value" : "Male"
           },
           {
               "Key" : "BirthDate",
               "Value" : "8/20/1964"
           },
           {
               "Key" : "Name",
               "Value" : "Success"
           }
       ]
   },
   "GroupID" : "84",
   "ClientID" : "8"
}

I did some poking around the JSON.stringify method but I couldn't find a guide that outlined how I'd go about doing this. Thanks so much for the help :) much appreciated

Was it helpful?

Solution

Since its an array of objects, just create your object:

var newObject = {
           "Key" : "BarcodePayload",
           "Value" : val
       }

And push to the array:

POSTOBJ.Subject.BiographicData.push(newObject);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top