Hello, please tell me how can I convert a string into an array of objects? There is a string

{ _id: 57fa6bd8cc9b4a17d085f53a, title: 'max', private: true }, { _id: 57fa6e10b8455715f48e793b, title: 'room1', private: false }, { _id: 57fa6e25b8455715f48e793c, title: 'room2', private: false } 

Team

 var test = JSON.parse(str); 

Returns an error

SyntaxError: Unexpected token _.

If JSON.parse run JSON.stringify() before JSON.parse , then there is no error, but the exact same string is returned.

Update

I get a list of rooms from the database and send it to the render.

 if (req.user){ mongoose.model('Room') .find() .select('title private') .exec(function(err,rooms){ roomsObj = rooms; res.render('chat.ejs', { isAuthenticated: req.isAuthenticated(), user: req.user ? req.user : null, rooms: roomsObj }); }); } else res.json("You should log in!"); 

Unfortunately, I did not understand how to transfer data using sockets from the route.js file to server.js. I decided to try to transfer to the client and from the client already on server.js. Since the object in this form (<% = roomsObj%>) is not transmitted, we had to shove it into a string. Client code

socket.emit('send_rooms', rommsObj = '<%= roomsObj %>');

  • one
    You have the wrong _id , i.e. without ', which indicates the number in this field, and here there are both numbers and letters, which is invalid json. If there are only numbers in _id , then everything is ok. - MrFylypenko pm
  • In general, your json is similar to the answer from mongodb, and see the code that forms this line? - Naumov
  • @Naumov This is the answer from the Mongo) Updated the question. - Maxim Cherevatov
  • I understand you have a nodejs? You need to encode this object in json and then output it, or does it come to json as it looks before sending it to the page? - Naumov
  • @Naumov I retrieve and send an object from the database, the object also arrives at the client, but in the socket.emit line ('send_rooms', rommsObj = '<% = roomsObj%>'); (Sends the data already to the file I need) you cannot send an object, and you have to put it into a string, that is, in quotes '' - Maxim Cherevatov

2 answers 2

actually found a solution in the chat, it is necessary to convert the object to json and output it without translation into an html entity

 socket.emit('send_rooms', rommsObj = <%- JSON.stringify(roomsObj) %>); 

    JSON format is quite strict.

    It strictly states that keys are strings, and strings must be in double quotes.

    In addition, json does not have the guid type, so the values ​​of the id fields should be in the form of strings, that is, also framed in double quotes, the same with the values ​​of the title field

     console.log( JSON.parse(`[{ "_id": "57fa6bd8cc9b4a17d085f53a", "title": "max", "private": true }, { "_id": "57fa6e10b8455715f48e793b", "title": "room1", "private": false }, { "_id": "57fa6e25b8455715f48e793c", "title": "room2", "private": false }]`) ); 

    • 2
      You do not think that your answer is hasty, since the person does not form json with his hands, but somewhere in the backend - Naumov
    • @Naumov, no, it does not seem. The answer is about the specific cause of the error. It doesn’t matter why it’s just such a string, it’s important why they are wrong. - Grundy