Luke Grech
Luke Grech

Reputation: 185

Converting MySQL queried JSON to Object

I have been struggling to convert the MySQL query JSON object below into a normal object. Any ideas on how to go about this? I have been trying for a long time but cant seem to find a solution. Thanks for your time.

{"lectures":"{\"id\":1,\"lecture_id\":9,\"school\":\"University\",\"location\":\"London\",\"class\":\"21\",\"subject\":\"Advanced Maths\",\"teacher_name\":\"John Andrews\",\"teacher_email\":\"[email protected]\",\"start_time\":\"18:00\",\"end_time\":\"21:00\",\"date\":\"Wed Apr 14 2021\",\"room_uuid\":\"0b762ce4-eb04-4068-a199-f32bdb694d47\",\"room_password\":\"pw\",\"additional_notes\":\"Bring protractor\"}"}

My code is below.

con.query("SELECT column FROM table", (err, latestEnrolment) => {
        if(err) throw err;
    });

    try{
        await res.render('ejs', { 
            latestEnrolment
        });
    }catch(err){
        console.log(err);
    }

Upvotes: 1

Views: 186

Answers (1)

Sneha Singh
Sneha Singh

Reputation: 143

Try this -

let lecturesString = object['lectures'].replace("\\", "")

let lecturesObj = JSON.parse(lecturesString)

console.log(lecturesObj)

Upvotes: 1

Related Questions