string to json in node js code example

Example 1: Javascript object to JSON string

var person={"first_name":"Tony","last_name":"Hawk","age":31};
var personJSONString=JSON.stringify(person);

Example 2: node string to json

const json = '{"result":true, "count":42}';
const obj = JSON.parse(json);

Example 3: javascript parse json

var jsonPerson = '{"first_name":"billy", "age":23}';
var personObject = JSON.parse(jsonPerson); //parse json string into JS object

Example 4: if json then parse

function isJson(str) {
    try {
        let value = JSON.parse(str);
      	return value
    } catch (e) {
        return str;
    }
}