Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to JSON.stringify an array of objects

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.

enter image description here

Added screen capture of the clients array containing WebSocket objects. enter image description here

How do I correctly "stringify" {key: arrayOfObjects[{key:value,key:{}},{},{}]}?

like image 214
crmepham Avatar asked Jan 29 '16 10:01

crmepham


People also ask

Can you JSON Stringify an array of objects?

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.

What happens when you JSON Stringify an array?

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.

Does JSON Stringify work on objects?

JSON. stringify() will encode values that JSON supports. Objects with values that can be objects, arrays, strings, numbers and booleans.

How do you parse an array of JSON objects?

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.

How do you stringify a JSON object in JavaScript?

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.

How to convert an array to a string in JSON?

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.

What are the types of arrays in JSON?

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

What is the difference between JSON stringify and JSON parse?

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.


1 Answers

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...

like image 128
Shaun Sharples Avatar answered Oct 29 '22 04:10

Shaun Sharples