1

I have a response coming back to my client that is in a javascript format. I have a table that I'm loading that expects the field names to be cased a specific way. Is it possible to map a key in the json to an field name in the json object?

Here is what I'm doing now:

this.resArray = Array;
this.results = new this.resArray();  // This object is using Phone, not phone 
var dataFromServerJSON = $.parseJSON(dataFromServer);  // dataFromServer has phone:3127789342
$.merge(app.Client.view.results, dataFromServerJSON)
user2197446
  • 1,065
  • 3
  • 15
  • 31
  • Please provide more information. What is the input and the expected output? But in general, yes, it is possible to rename the property of an object: [Rename the property names and change the values of multiple JSON objects](http://stackoverflow.com/q/10819863/218196) – Felix Kling Oct 30 '15 at 17:48

1 Answers1

1

I'm guessing that if your JSON response looks like this

 [
       {
           "name" : "Larry",
           "dateofbirth": "08/20/1988"
       },
       {
           "name" : "Sarah",
           "dateofbirth": "03/23/1991"
       }
 ]

You need your JSON object to look like this

 [
        {
           "name" : "Larry",
           "dateOfBirth": "08/20/1988"
       },
        {
           "name" : "Sarah",
           "dateOfBirth": "03/23/1991"
       }
 ]

If this is the case, simply parse through the response and assign its values to a new JSON object initialized to newArr = []

  var newObj = [];
  for(var i = 0; i < response.length; i++) {
      newObj[i] = {};          
      newObj[i]["name"] = response[i]["name"];
      newObj[i]["dateOfBirth"] = response[i]["dateofbirth"];          
   }
Divyanth Jayaraj
  • 950
  • 4
  • 12
  • 29