0

I want to validate a JSON object without the help of any libraries in nodejs.

5
  • 2
    Please define what do you mean by validate and post a sample json Commented May 7, 2018 at 9:32
  • And what did you find so far and why it doesn't fit your needs? Commented May 7, 2018 at 9:32
  • I just want to know if the JSON object is valid without errors. there are a lot of libraries which does the job I want to do it without using them. Commented May 7, 2018 at 9:35
  • i just want to check if the json objcet passed is without errors and in proper syntax. Commented May 7, 2018 at 9:36
  • Just use JSON.prase. It will throw error if invalid json. Look at the link provided by @ChrisR Commented May 7, 2018 at 9:37

1 Answer 1

3

Just by parsing it, using JSON.parse function:

function isValidJSON(text){
    try{
        JSON.parse(text);
        return true;
    }
    catch (error){
        return false;
    }
}


console.log(isValidJSON("hello")); // false
console.log(isValidJSON("{}")); // true
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.