0

I see there is a problem to get a String name from JSON object's name. I need to parse this kind on JSON response from server.

var response = {
  hopaopGmailsjIpW: {
    GmailsjIpW_totalEmails_count: 133,
    GmailsjIpW_state: 1
  },
  hopaopGmail4y4yu: {
    Gmail4y4yu_totalEmails_count: 156,
    Gmail4y4yu_state: 1
  }
}

It is not an Array, but the object with inner objects. i need to parse an inner ojects name and add additional values to each object.

i want be able to do something like this:

for(var i =0; i < response.length; i++){
  response[i].username = parseUsernameFromString(response[i]);
  response[i].service = parseServiceFromString(response[i]);
  response[i].id = parseIdString(response[i]);
}

(and also state for each task)

So the question is: What is the best way to make it?

UPDATE this is exactly what i have for now:

for(var key in response){
    if(stringContains(response[key], "Gmail")) { response[key].service = "Gmail";}
        console.log("task name: "+ response[key].service);
}
function stringContains(originalString, searchString){
        if(originalString.indexOf(searchString) > -1){
            return true
        }
            else return false;
}
user1935987
  • 3,136
  • 9
  • 56
  • 108
  • 1
    Your first block of code is not JSON, nor is it JavaScript. You'd need to write a custom parser for it (start reading [here](https://en.wikipedia.org/wiki/Lexical_analysis)). It would be better to fix the server so the response is actually JSON. – Quentin Jun 24 '16 at 09:53
  • The thing is it is a request from some implementation of dataCache on server side, changing server part is too much work, doesn't worth. also i have this situation only in one place in my app. – user1935987 Jun 24 '16 at 09:55
  • right, comma is missing, sorry, – user1935987 Jun 24 '16 at 09:56
  • Are you asking how to loop through the object, or how to write the `parseUsernameFromString` etc functions? The question isn't very clear. – JJJ Jun 24 '16 at 10:00
  • 2
    That structure is just truly, truly bizarre. It really needs fixing. The properties of the inner objects should have consistent names, for one. Basic principles, people. – T.J. Crowder Jun 24 '16 at 10:07
  • @T.J.Crowder Agree with this. – Praveen Kumar Purushothaman Jun 24 '16 at 10:08
  • 'i need to parse an inner ojects name and add additional values to each object.' whats what i asking. The structure of objects is like this because it is an object drom datacache in back end which is working with multithreaded async tasks and each group, user, task need to have their id's.. anyway thats not the issue. – user1935987 Jun 24 '16 at 10:22

2 Answers2

2

For walking through Objects, you need to use for ... in loop.

The real problem is: There's a , missing in your code. See the fixed working snippet:

Snippet

var response_Parsed = {
  hopaopGmailsjIpW: {
    GmailsjIpW_totalEmails_count: 133,
    GmailsjIpW_state: 1,
    service: 'Gmail',
    username: 'hopaop',
    id: 'sjIpW'
  },
  hopaopGmail4y4yu: {
    Gmail4y4yu_totalEmails_count: 156,
    Gmail4y4yu_state: 1,
    service: 'Gmail',
    username: 'hopaop',
    id: '4y4yu'
  }
};
for (id in response_Parsed) {
  console.log("----");
  if (id.indexOf("Gmail") > -1) {
    console.log("We have Gmail: " + id);
    console.log("UniqueName:    " + id.replace("hopaopGmail", ""));
    console.log("Username:      " + response_Parsed[id].username);
    console.log("Email Count:   " + response_Parsed[id][id.replace("hopaop", "") + "_totalEmails_count"]);
  }
  else
    console.log("We don't have Gmail: " + id);
}

And also the right way to enumerate the through the keys of the objects, is by using Object.keys.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
1

If the response is a String like you wrote, you should first parse the JSON-String into an Object (if you're using a library like jQuery, it's probably already a JSON, as this conversion is done by jQuery automatically):

var obj = JSON.parse(responseString);

Afterwards you may iterate through it like posted above:

for (var key in obj) {
    console.log("key", key, "value", obj[key]);
}
MattDiMu
  • 4,873
  • 1
  • 19
  • 29