1

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

1
  • That is JavaScript, not JSON. Commented Mar 19, 2014 at 15:20

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Worked like a charm. Exactly what I was missing. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.