I am attempting to JSON.stringify()
the following key/value pair, where the value is an array of objects.
var string = JSON.stringify({onlineUsers : getUsersInRoom(users, room)});
This is incorrect and gives the following error:
var string = JSON.stringify({onlineUsers : getUsersInRoom(users, room)});
^
TypeError: Converting circular structure to JSON
This is the method:
function getUsersInRoom(users, room) {
var json = [];
for (var i = 0; i < users.length; i++) {
if (users[i].room === room) {
json.push(users[i]);
}
}
return json;
}
Added users
data structure:
[
{
id:1,
username:"",
room:"room 1",
client: {
sessionId:1,
key:value
}
},
{
// etc
}
]
Added function to add user to users array.
function addUser(client) {
clients.push(client);
var i = clients.indexOf(client);
if (i > -1) {
users.push({
id : i,
username : "",
room : "",
client : clients[i]
});
}
}
Added screen capture of JavaScript array containing an object as well as key/value pairs inside an object.
Added screen capture of the clients array containing WebSocket objects.
How do I correctly "stringify" {key: arrayOfObjects[{key:value,key:{}},{},{}]}
?
The JSON. stringify method converts a JavaScript object or value to a JSON string. It can optionally modify or filter values if a replacer function/array is specified.
The JSON.stringify() method converts a JavaScript object or value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.
JSON. stringify() will encode values that JSON supports. Objects with values that can be objects, arrays, strings, numbers and booleans.
Use the JSON. parse() method to pase a JSON array, e.g. JSON. parse(arr) . The method parses a JSON string and returns its JavaScript value or object equivalent.
Stringify a JavaScript Object. Imagine we have this object in JavaScript: var obj = { name: "John", age: 30, city: "New York" }; Use the JavaScript function JSON.stringify () to convert it into a string. var myJSON = JSON.stringify(obj); The result will be a string following the JSON notation.
The JSON.stringify () function converts the JSON array to a string format to make it in a human-readable format. We have created a JSON objects array.
1 Arrays as JSON Objects. Arrays in JSON are almost the same as arrays in JavaScript. In JSON, array values must be of type string, number, object, array, boolean or null. 2 Arrays in JSON Objects 3 Accessing Array Values 4 Looping Through an Array 5 Nested Arrays in JSON Objects 6 Modify Array Values 7 Delete Array Items
JSON.stringify - converts JS objects into JSON JSON.parse - converts JSON back into a JS object The JSON.stringify method converts a JavaScript object or value to a JSON string. It can optionally modify or filter values if a replacer function/array is specified. The value is the value to convert to a JSON string.
var data = { };
data.onlineUsers = getUsersInRoom();
var string = JSON.stringify(data);
Would this work for you?
edit
I just noticed your error is circular type, your user or room object is probably creating a circular reference.
User > Room > User > Room etc...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With