1

I'm receiving data on my nodejs app from a cordova app via a jQuery ajax call and it's formatted as

{
     "network[msisdn]": "+254738XXXXXX",
      "network[country]": "ke",
      "network[roaming]": "false",
      "network[simState]": "Ready",
      "network[network]": "HSPA",
      "network[simSerial]": "89254031021032011310",
      "network[subscriber]": "639031023201131",
      "network[service]": "GSM"
}

instead of the usual

{
  network: {    
              "msisdn" : "",
               ...
           }
}

I can loop through the object in the cordova phone app while accessing the nested keys like objectName.network.msisdn but I cannot once I receive the data in my nodejs backend.

I am posting the data as shown below

$.ajax({
         url: 'http://'+$scope.api.host+':'+$scope.api.port+'/notices',
         method: 'POST',
         dataType: 'json',
         data: $scope.storage.history[0]
      }).then(function(response){

          //! STORE THE RESULT IN THE RELEVANT OBJECT 
          $scope.storage.history[nextPos].locale = response;
          alert(JSON.stringify(response));

      }); 

I would like to access the sub keys from the object.

I have tried Json.Parse(Json.stringify(objectName)) before posting the data,

I have also tried to post without the json dataType in the jQuery ajax call,

I have tried to JSON.parse( ) the object in the back end all to no avail.

I really appreciate your assistance.

2
  • I dont understand, do you need the object to be formatted this way (network[msisdn])? Commented Aug 20, 2016 at 11:24
  • Thanks for the concern Ahmad Bamieh, I need it formatted as network.msisdn but I am getting an object with the key "network[msisdn]" instead Commented Aug 20, 2016 at 12:19

1 Answer 1

3

If you can't change how the data is returned, you can access it using string notation. Here is an example of using string notation and also a function that you could use to convert it to a nested object so that you can use dot notation.

var exampleData = {
  "network[msisdn]": "+254738XXXXXX",
  "network[country]": "ke",
  "network[roaming]": "false",
  "network[simState]": "Ready",
  "network[network]": "HSPA",
  "network[simSerial]": "89254031021032011310",
  "network[subscriber]": "639031023201131",
  "network[service]": "GSM",
  "simpleKey": "simpleValue"
}

console.log(exampleData['network[country]']); // Logs -> ke

// This converts the keys in place
// it can be modified to return a new object instead
function convertKeys(data) {
  var pieces;
  for(var key in data) {
    if (data.hasOwnProperty(key)) {
      pieces = key.match(/(.+)\[(.+)]/);
      if (pieces) {
        data[pieces[1]] = data[pieces[1]] || {};
        data[pieces[1]][pieces[2]] = data[key];
        delete data[key];
      }
    }
  }
}

convertKeys(exampleData);
console.log(exampleData);
console.log(exampleData.network.simState); // Logs -> Ready

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @rdbya, your solution works as is but once I substitute it with my data, I get the error: ** data.hasOwnProperty is not a function **
@rdbya, thank you very much. I have worked out out by replacing that line of code with if(data[key]) as well as an if(pieces) ... Validation to prevent simple key value pairs from being manipulated.
@ianmin2 Glad you got it figured out. I updated my answer to include the changes you mentioned.
I just found out that I was getting the '.hasownproperty' is not defined error because I was using an arrow function as the parent function and they by default have no prototypes assigned to them.

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.