Lodash _.isJSON() Method
Last Updated :
13 Nov, 2023
Lodash _.isJSON() method checks whether the given value is a valid JSON or not and returns the corresponding boolean value.
Syntax:
_.isJSON( value );
Parameters:
This method takes single parameter as mentioned above and described below:
- value: Given value to be checked for JSON.
Return Value:
This method returns a Boolean value true if the given value is valid JSON, else false.
Note: This will not work in normal JavaScript because it requires the lodash.js contrib library to be installed. Lodash.js contrib library can be installed using “npm install lodash-contrib”.
Example 1: In this example, the lodash-contrib library’s _.isJSON()
method is used to check if a given value is a valid JSON string, and it confirms that the provided value is a JSON string, returning true
.
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
Example 2: In this example we use the lodash-contrib library’s _.isJSON()
method to check if a provided value is valid JSON, returning false
in this case.
Javascript
let _ = require( 'lodash-contrib' );
console.log( "The Value is JSON : " +_.isJSON(
{GeeksforGeeks : "A Computer Science portal for Geeks" }));
|
Output:
The Value is JSON : false
Example 3: In this example, _.isJSON()
from lodash-contrib checks if the provided value (an array) is valid JSON, returning false
.
Javascript
let _ = require( 'lodash-contrib' );
console.log( "The Value is JSON : " + _.isJSON([ "gfg" ]));
|
Output:
The Value is JSON : false
Share your thoughts in the comments
Please Login to comment...