Transform this result in json valid.
"{\"name\":\"log\",\"hostname\":\"denis-Latitude-E7470\",\"pid\":1007,\"level\":30,\"conextion\":\"DBA MongDB: \[32m%s\[0m\",\"msg\":\"online\",\"time\":\"2019-12-06T13:50:42.510Z\",\"v\":0}"
Transform this result in json valid.
"{\"name\":\"log\",\"hostname\":\"denis-Latitude-E7470\",\"pid\":1007,\"level\":30,\"conextion\":\"DBA MongDB: \[32m%s\[0m\",\"msg\":\"online\",\"time\":\"2019-12-06T13:50:42.510Z\",\"v\":0}"
Just parse it via JSON.parse()
to convert the string into a JSON object.
let jsonString = "{\"name\":\"log\",\"hostname\":\"denis-Latitude-E7470\",\"pid\":1007,\"level\":30,\"conextion\":\"DBA MongDB: \[32m%s\[0m\",\"msg\":\"online\",\"time\":\"2019-12-06T13:50:42.510Z\",\"v\":0}";
console.log(JSON.parse(jsonString));
let brokenJsonString = '{ "key": "<div class="coolCSS">some text</div>" }';
try {
console.log(JSON.parse(brokenJsonString));
} catch (e) {
console.log("Exception thrown when parsing.", e.toString());
}
Be careful when parsing strings because (as @Fallenreaper noted) malformed or invalid JSON is going to result in an error thrown. So wrap your JSON.parse()
with try...catch
statement (more about it here).
Broken/malformed can be handled with libraries like this but use them when you absolutely need it and always read the docs.