Open In App
Related Articles

Check if a string is a valid JSON string using JavaScript

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

We will be given a string and we need to validate whether the given string is a valid JSON string or not. JSON string is the same as a javaSCript object and it must follow the syntax of that object. If the syntax is correct then we have to return true or we have to return false.

Methods to Check if a String is Valid JSON String:

Method 1: Using JSON.parse() method

The JSON.parse() method in JavaScript is used to parse a JSON string which is written in a JSON format and returns a JavaScript object

Syntax:

JSON.parse( text, function);

Example 1: This example checks the validity of JSON string by the JSON.parse() method by creating a function. 

Javascript

// Input sting
let str = '{ "prop_1": "val_1", "prop_2": "val_2" }';
console.log("Input string is : " + str);
 
// Function to test string
function testJSON(text) {
    if (typeof text !== "string") {
        return false;
    }
    try {
        JSON.parse(text);
        return true;
    } catch (error) {
        return false;
    }
}
 
// Function to display output
function gfg_Run() {
    console.log(testJSON(str));
}
 
// Function call
gfg_Run();

                    

Output
Input string is : { "prop_1": "val_1", "prop_2": "val_2" }
true

Example 2: This example is returning false because the property value val_1 is not a string. 

Javascript

// Input sting
let str = '{ "prop_1": val_1, "prop_2": "val_2" }';
console.log("Input string is : " + str);
 
// Function to test string
function isJSON(str) {
    try {
        return JSON.parse(str) && !!str;
    } catch (e) {
        return false;
    }
}
 
// Function to display output
function gfg_Run() {
    console.log(isJSON(str));
}
 
// Function call
gfg_Run();

                    

Output
Input string is : { "prop_1": val_1, "prop_2": "val_2" }
false

Example 3: This example is returning true by using the JSON.parse() method along with typechecking of the text. 

Javascript

// Input sting
let str = '{ "prop_1": 1, "prop_2": "val_2" }';
console.log("Input string is : " + str);
 
// Function to test string
function isJSON(str) {
    try {
        return JSON.parse(str) && !!str;
    } catch (e) {
        return false;
    }
}
// Function to display output
function gfg_Run() {
    console.log(isJSON(str));
}
 
// Function call
gfg_Run();

                    

Output
Input string is : { "prop_1": 1, "prop_2": "val_2" }
true

Method 2: Using JSON.stringify() method

The JSON.stringify() method in Javascript is used to create a JSON string out of it.

Syntax:

JSON.stringify(value, replacer, space);

Example: In this example, we will try to convert the given object to a string using JSON.stringify() method

Javascript

// Input sting
let str = '{ "prop_1": "val_1", "prop_2": "val_2" }';
console.log("Input string is : " + str);
 
// Function to test string
function isJSON(str) {
    try {
        JSON.stringify(JSON.parse(str));
        return true;
    } catch (e) {
        return false;
    }
}
// Function to display output
function gfg_Run() {
    console.log(isJSON(str));
}
 
// Function call
gfg_Run();

                    

Output
Input string is : { "prop_1": "val_1", "prop_2": "val_2" }
true

Method 3: Using Lodash _.isJSON() Method

In this approach, we are using Lodash _.isJSON() method that returns the boolean value that is true if the passes value is a JSON string else it returns false.

Example: In this example, we are using the Lodash _.isJSON() method.

Javascript

// Defining lodash contrib variable
let _ = require('lodash-contrib');
 
// Checking for _.isJSON() method
console.log("The Value is JSON : " +_.isJSON(
'{"GeeksforGeeks" : "A Computer Science portal for Geeks"}'));

                    

Output:

The Value is JSON : true


Last Updated : 07 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads