seantrant
6/22/2017 - 3:03 PM

Creating a Javascript object & converting to JSON String

Creating a Javascript object & converting to JSON String

function sendPick(){    //TODO need loop for multiple
  var items = [];
  var pickJSON = {};
  var i = 0;
  pickJSON.id = 'x';
  $('.review-product-list > li').each(function(){
    var singleItem = {};
    var sku = $(this).data('sku');
    var count = $(this).data('count');

    singleItem.editidstring = sku;
    singleItem.count = count;
    items[i] = singleItem;
    console.log(i)
    i++;
  });
  pickJSON.items = items;

  console.log(JSON.stringify(pickJSON)); // will output valid json

  // sample output json below:
  // {
  //   "id":  "1234",
  //   "item": [
  //       {
  //           "editidstring":  "98765",
  //           "count":   1
  //       }
  //   ]
  // }
}