Thursday, July 4, 2024
HomeLanguagesJavascriptCheck if a string is a valid JSON string using JavaScript

Check if a string is a valid JSON string using JavaScript

In order to check the validity of a string whether it is a JSON string or not, We’re using the JSON.parse()method with a few variations. JSON.parse() This method parses a JSON string and constructs the JavaScript value or object specified by the string. A reviver function may be provided to do a change on the resulting object before it’s returned. It can be done using the following methods:

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 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();


Calisto Chipfumbu
Calisto Chipfumbuhttp://cchipfumbu@gmail.com
I have 5 years' worth of experience in the IT industry, primarily focused on Linux and Database administration. In those years, apart from learning significant technical knowledge, I also became comfortable working in a professional team and adapting to my environment, as I switched through 3 roles in that time.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments