1

I have an object which was deserialized from JSON:

[Log] {"Rob7":{"lastSeenAt":null,"state":"online","userId":"Rob7"},"Techyudh_rob63":{"lastSeenAt":"2018-07-18T17:45:56Z","state":"offline","userId":"Techyudh_rob63"},"Teacher4":{"lastSeenAt":null,"state":"online","userId":"Teacher4"}} (chat, line 965)

I want to fetch the state of each user from it. I am trying this.

var j = (JSON.stringify(val.presenceStore.store));
var json = $.parseJSON(j);
$(json).each(function(i, val) {
  $.each(val, function(k, v) { 
    console.log(k + " : " + v);
  });
});

However, it shows, Rob7: [object Object]. I am new with JSON. Can anyone please help?

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
Rob
  • 164
  • 1
  • 2
  • 12
  • console.log(k+" : "+ JSON.stringify(v)); or put comma in console. – Sumesh TG Jul 20 '18 at 08:23
  • Firstly, note that what you are working with to retrieve the values is an object, not JSON. Secondly, you don't want to use jQuery for this. jQuery is primarily intended as a DOM manipulation tool. For reading data structures, such as an object like in this case, you need plain old JS. I amended the title and tags as such – Rory McCrossan Jul 20 '18 at 08:27
  • The way you have your console message written, you're creating a string (text). So the browser is doing it's version of printing out the text equivalent is the JSON (JavaScript OBJECT notation). But browsers are pretty smart, if you just logged without "adding" (technically concatenating) the text of " : " it should print out the actual object not just it's string representation of [ object object ]. As Sumesh pointed out, if your log was console.log(k, v) you're invoking the actual objects not the string representation – Doug Jul 20 '18 at 08:30
  • 2
    Why are you turning an object into JSON and then turning that JSON into an object? In your code, `var json == val.presenceStore.store` - you've serialized and then immediately deserialized. – Reinstate Monica Cellio Jul 20 '18 at 08:36
  • @Archer possibly a [clone routine](https://stackoverflow.com/a/10869248/542251) – Liam Jul 20 '18 at 08:44
  • @Liam Yes, that's possible but until OP answers we don't know. They may be doing it unnecessarily, which is more likely. – Reinstate Monica Cellio Jul 20 '18 at 08:46

2 Answers2

1

You can do that in plain JavaScript, no need of jQuery. After parsing the JSON, just loop over the returned value using Object.entries( ... ).forEach and you are set.

var data = {
  "Rob7": {
    "lastSeenAt": null,
    "state": "online",
    "userId": "Rob7"
  },
  "Techyudh_rob63": {
    "lastSeenAt": "2018-07-18T17:45:56Z",
    "state": "offline",
    "userId": "Techyudh_rob63"
  },
  "Teacher4": {
    "lastSeenAt": null,
    "state": "online",
    "userId": "Teacher4"
  }
};

Object.entries(data).forEach(([k, v]) => {
  console.log(k, v.state);
});
31piy
  • 23,323
  • 6
  • 47
  • 67
1

Your problem here is that you are trying to append an object to a string in the line:

console.log(k + " : " + v);

The object inside v can't be appended as a string to the console so that's why you got : Rob7: [object Object]

Solution:

If you just separate the key and value logging statements it will work perfectly:

console.log(k + " : ");
console.log(v);

Another option is to get back a string from the v object, using JSON.stringify():

console.log(k + " : " + JSON.stringify(v));
cнŝdk
  • 31,391
  • 7
  • 56
  • 78