How to JSON.stringify an array of objects
<p>I am attempting to <code>JSON.stringify()</code> the following key/value pair, where the value is an array of objects.</p>

<pre><code>var string = JSON.stringify({onlineUsers : getUsersInRoom(users, room)});
</code></pre>

<p>This is incorrect and gives the following error:</p>

<blockquote>
  <p>var string = JSON.stringify({onlineUsers : getUsersInRoom(users, room)});</p>

<pre><code>                ^
</code></pre>
  
  <p>TypeError: Converting circular structure to JSON</p>
</blockquote>

<p>This is the method:</p>

<pre><code>function getUsersInRoom(users, room) {
    var json = [];
    for (var i = 0; i &lt; users.length; i++) {
        if (users[i].room === room) {

            json.push(users[i]);
        }
    }
    return json;
}
</code></pre>

<p>Added <code>users</code> data structure:</p>

<pre><code>[
 {
     id:1,
     username:"",
     room:"room 1",
     client: {
         sessionId:1,
         key:value
     }
 },
 {
     // etc
 }
]
</code></pre>

<p>Added function to add user to users array.</p>

<pre><code>function addUser(client) {
    clients.push(client);
    var i = clients.indexOf(client);
    if (i &gt; -1) {
        users.push({
            id : i,
            username : "",
            room : "",
            client : clients[i]
        });
    }
}
</code></pre>

<p>Added screen capture of JavaScript array containing an object as well as key/value pairs inside an object.</p>

<p><a href="https://i.stack.imgur.com/5OMVI.png" rel="noreferrer"><img src="https://i.stack.imgur.com/5OMVI.png" alt="enter image description here"></a></p>

<p>Added screen capture of the clients array containing WebSocket objects.
<a href="https://i.stack.imgur.com/zIUad.png" rel="noreferrer"><img src="https://i.stack.imgur.com/zIUad.png" alt="enter image description here"></a></p>

<p>How do I correctly "stringify" <code>{key: arrayOfObjects[{key:value,key:{}},{},{}]}</code>?</p>