Check if a string is a valid JSON string using JavaScript
Last Updated :
07 Dec, 2023
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:
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
let str = '{ "prop_1": "val_1", "prop_2": "val_2" }' ;
console.log( "Input string is : " + str);
function testJSON(text) {
if ( typeof text !== "string" ) {
return false ;
}
try {
JSON.parse(text);
return true ;
} catch (error) {
return false ;
}
}
function gfg_Run() {
console.log(testJSON(str));
}
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
let str = '{ "prop_1": val_1, "prop_2": "val_2" }' ;
console.log( "Input string is : " + str);
function isJSON(str) {
try {
return JSON.parse(str) && !!str;
} catch (e) {
return false ;
}
}
function gfg_Run() {
console.log(isJSON(str));
}
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
let str = '{ "prop_1": 1, "prop_2": "val_2" }' ;
console.log( "Input string is : " + str);
function isJSON(str) {
try {
return JSON.parse(str) && !!str;
} catch (e) {
return false ;
}
}
function gfg_Run() {
console.log(isJSON(str));
}
gfg_Run();
|
Output
Input string is : { "prop_1": 1, "prop_2": "val_2" }
true
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
let str = '{ "prop_1": "val_1", "prop_2": "val_2" }' ;
console.log( "Input string is : " + str);
function isJSON(str) {
try {
JSON.stringify(JSON.parse(str));
return true ;
} catch (e) {
return false ;
}
}
function gfg_Run() {
console.log(isJSON(str));
}
gfg_Run();
|
Output
Input string is : { "prop_1": "val_1", "prop_2": "val_2" }
true
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
let _ = require( 'lodash-contrib' );
console.log( "The Value is JSON : " +_.isJSON(
'{"GeeksforGeeks" : "A Computer Science portal for Geeks"}' ));
|
Output:
The Value is JSON : true
Share your thoughts in the comments
Please Login to comment...