Bilal Usean Bilal Usean - 6 months ago 28
JSON Question

Json length is always undefined in node js

I am trying to iterate simple json array, but it always return the length of the array is undefined.

var chatmessage = {};
...................
...................
socket.on('listmessage', function(mesg){
chatmessage={"message":"Hello", "to":"sjdfjhsdf"};
});

socket.on('private', function(mesg){
console.log(chatmessage.length+' - '+chatmessage.message +' - '+ chatmessage.to);
});


when private event get trigger it returns

undefined - Hello - sjdfjhsdf


I think it consider length key word as a json array key like {"length":40}.

I have tried Object.keys(chatmessage).length but it returns the total number key value(2) but I have only one record.

What is the right way to iterate json in node js

Answer

I am trying to iterate simple json array

Two issues there:

  1. That's not JSON. JSON is a textual notation for data exchange. If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON. It's a JavaScript object initializer.

  2. It doesn't define an array, it defines an object.

Objects don't have a length property. If you want your chatmessage to say how many properties it has, you'll have to add a third property explicitly (which then raises the question of whether the value should be 2 or 3 :-) ). Or alternately, you could make it an array of objects with "key" and "value" properties, but that would be awkward to work with.

If you need to, you can determine how many properties an object has in a couple of ways:

  1. Object.keys(obj).length will tell you how many own, enumerable properties the object has (ignoring any with Symbol names, if you're using ES2015). The number will not include any inherited properties or any non-enumerable properties. The answer in your example would be 2.

  2. Object.getOwnPropertyNames(obj).length) will tell you how many own properties the object, has regardless of whether they're enumerable (but again ignoring any with Symbol names). The number will not include any inherited properties or any non-enumerable properties. The answer in your example would again be 2 as your object has no non-enumerable properties.

I have tried Object.keys(chatmessage).length but it returns the total number key value(2) but I have only one record.

As I said above, Object.keys will tell you how many own enumerable properties the object has. If you're trying to find out how many objects there are, the answer is 1.

If your goal is to send an array of chat messages, then you want to create that like this:

var chatmessages = [
    {"message":"Hello", "to":"sjdfjhsdf"}
];

That defines an array with one entry: A single object representing a chat message. Multiple ones would be separated with ,:

var chatmessages = [
    {"message":"Hello", "to":"sjdfjhsdf"},         // First message
    {"message":"Hello again", "to":"sjdfjhsdf"}    // Second message
];

Note that if you're doing that, your verb should probably be listmessages (plural), not listmessage. (I mention this in case your native language handles plurals differently from English; there are a lot of different ways plurals are handled in the various human languages around the planet. :-) )