JSON.stringify problems

In the code below, I am trying to send a JSON encoded javascript object.

However, when the object is json "stringified", it removes the eventcode, price and WaitingList values.
In the JSON result (json_data), all I see is the firstname, lastname, email for each particpant that was added.

How do I prevent this from happening. The eventcode, price and WaitingList values are global for all participants.
    window.particpants = [];
    window.participants['eventcode']   = "GEVENT";
    window.participants['price']       = 99.00;
    window.participants['WaitingList'] = "";

    if(waiting_list_flag) {
      window.participants['WaitingList'] = "YES";
    } else {
      window.participants['WaitingList'] = "NO";
    }

    $('#next_btn').click(function() {
      addParticipant(); // adds the last entered participant info to the participants object
      var url = AJAX_URL_ADD
      json_data = JSON.stringify(window.participants); // json encode the participants

      $.ajax({async: false, 
              type:'POST',
              url: url, 
              data: json_data, 
              dataType: 'json', 
              contentType: "application/json; charset=utf-8", 
              success: function(data){
                if(data.result=='success') {
                  window.location = GOTO_URL;
                } else {
                  //display error
                  $('.error_message').html(data.message);
                } // if(data.result=='success')
              } // success function()
      }); // $.ajax
    }); // $('#next_btn').click

  // also called from another button click to add more participants
  function addParticipant() {
    var participant = {};
    participant.firstname   = $('#FirstName').val();
    participant.lastname    = $('#LastName').val();
    participant.email       = $('#Email').val();
    window.participants.push(participant);
  } // function addParticipant()

Open in new window

LVL 26
Eddie ShipmanAll-around developerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
Try this :


window.participants = {};
    window.participants['eventcode']   = "GEVENT";
    window.participants['price']       = 99.00;
    window.participants['WaitingList'] = "";
    window.participants['participants'] = [];

    if(waiting_list_flag) {
      window.participants['WaitingList'] = "YES";
    } else {
      window.participants['WaitingList'] = "NO";
    }

    $('#next_btn').click(function() {
      addParticipant(); // adds the last entered participant info to the participants object
      var url = AJAX_URL_ADD
      json_data = JSON.stringify(window.participants); // json encode the participants

      $.ajax({async: false, 
              type:'POST',
              url: url, 
              data: json_data, 
              dataType: 'json', 
              contentType: "application/json; charset=utf-8", 
              success: function(data){
                if(data.result=='success') {
                  window.location = GOTO_URL;
                } else {
                  //display error
                  $('.error_message').html(data.message);
                } // if(data.result=='success')
              } // success function()
      }); // $.ajax
    }); // $('#next_btn').click

  // also called from another button click to add more participants
  function addParticipant() {
    var participant = {};
    participant.firstname   = $('#FirstName').val();
    participant.lastname    = $('#LastName').val();
    participant.email       = $('#Email').val();
    window.participants.participants.push(participant);
  } // function addParticipant()

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Eddie ShipmanAll-around developerAuthor Commented:
window.participants.participants is undefined

Open in new window

If I leave it at
window.participants.push(participant)

Open in new window

I get
window.participants.push is not a function

Open in new window

0
leakim971PluritechnicianCommented:
did you made a copy/cut? (check line 5)

work fine here : http://jsfiddle.net/38jAn/1/
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Eddie ShipmanAll-around developerAuthor Commented:
Got it, missed that one. Also, modifying to this worked, is this better or should I do it like you did in the Fiddle?
    window.participants.eventcode   = "GEVENT";
    window.participants.price       = 99.00;
    window.participants.WaitingList = "";

Open in new window

0
leakim971PluritechnicianCommented:
no problem with this syntax, if you like it adopt it.

forexample["this-is-not-possible-with-the-other-syntax"] = 10; // you can't : forexample.this-is-not-possible-with-this-syntax = 10;

0
Eddie ShipmanAll-around developerAuthor Commented:
Got it, thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.