Open In App

Lodash _.isJSON() Method

Last Updated : 13 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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
// 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

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
// 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 : false

Example 3: In this example, _.isJSON() from lodash-contrib checks if the provided value (an array) is valid JSON, returning false.

JavaScript
// Defining lodash contrib variable 
let _ = require('lodash-contrib'); 

// Checking for _.isJSON() method
console.log("The Value is JSON : " + _.isJSON(["gfg"]));

Output:

The Value is JSON : false

Next Article

Similar Reads