-2

I can't seem to find whats wrong in this code. I keep getting Uncaught SyntaxError: Unexpected string in Chrome console.

var json = '{"shipment":{"weight":"'+a[0].value+'","value":"0","quantity":"'+a[1].value+'","insurance":"false","dimensions":{"length":"30","width":"2","height":"10"},"origin":{"country":"'+a[2].value+'","formattedAddress":"'+a[3].value+'","locality":"'+a[4].value+'","postalCode":"'+a[5].value+'","contact":"'+a[6].value+'","email":"'+a[7].value+'","comments":"'+a[8].value+'"},"destination":{"country":"'+a[9].value+'","formattedAddress":"'+a[10].value+'","locality":"'+a[11].value'","postalCode":"'+a[12].value+'","contact":"'+a[13].value+'","email":"'+a[14].value+'","comments":"'+a[15].value+'"},"vehicleType":"'+a[16].value+'"}}';
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Selmer
  • 3
  • 4
  • 1
    Double check all of your quotes and things. You're probably just missing a `+` sign or something. – gen_Eric May 19 '14 at 16:11
  • 9
    Why in the name of all that's holy are you appending to a JSON string? It would be much easier to create an object, make your amendments to that and then serialise as needed. – Rory McCrossan May 19 '14 at 16:11
  • 2
    Why are you manually building a JSON string? This is usually bad practice. Try to create it as an object (not a string). If you want JSON, then use `JSON.stringify()`. – gen_Eric May 19 '14 at 16:12
  • 1
    Basically, don't ever do string manipulation on serialised data. (You know, like [HTML](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags).) – lonesomeday May 19 '14 at 16:15
  • `...+a[11].value'","postalCode":"'...` <- There's your issue. It became obvious after using my IDE to replace `'+'` with `' +\n'`. – gen_Eric May 19 '14 at 16:18

1 Answers1

3

Please save yourself a thousand headaches and do this instead:

var data = {
  shipment: {
    weight:      a[0].value,
    value:       0,
    quantity:    a[1].value,
    insurance:   "false",
    dimensions:  { length: "30", width: "2", height: "10" },
    origin: {
      country:          a[2].value,
      formattedAddress: a[3].value,
      locality:         a[4].value,
      postalCode:       a[5].value,
      contact:          a[6].value,
      email:            a[7].value,
      comments:         a[8].value
    },
    destination: {
      country:          a[9].value,
      formattedAddress: a[10].value,
      locality:         a[11].value,
      postalCode:       a[12].value,
      contact:          a[13].value,
      email:            a[14].value,
      comments:         a[15].value
    },
    vehicleType: a[16].value
  }
};

var json = JSON.stringify(data);

Or, if you want your code to be a little bit maintainable down the road:

function addressDataFromArray(arr) {
  return {
    country:          a[0].value,
    formattedAddress: a[1].value,
    locality:         a[2].value,
    postalCode:       a[3].value,
    contact:          a[4].value,
    email:            a[5].value,
    comments:         a[6].value
  }
}

function shipmentDataFromArray(arr) {
  var originData      = addressDataFromArray( arr.slice(2, 8) ),
      destinationData = addressDataFromArray( arr.slice(9, 15) );

  return {
    weight:      arr[0].value,
    value:       0,
    quantity:    arr[1].value,
    insurance:   "false",
    dimensions:  { length: "30", width: "2", height: "10" },
    origin:      originData,
    destination: destinationData,
    vehicleType: arr[16].value
  }
}

var shipmentData = shipmentDataFromArray(a),
    json = JSON.stringify({ shipment: shipmentData });
Jordan Running
  • 102,619
  • 17
  • 182
  • 182