w3resource

JavaScript: Validate whether a given value type is pure json object or not

JavaScript validation: Exercise-7 with Solution

Validate Pure JSON Object

Write a JavaScript function to validate whether a given value type is a pure JSON object or not.

Sample Solution: -

HTML Code:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript function to validate whether a given value type is pure json object or not.</title>
</head>
<body>

</body>
</html>

JavaScript Code:

function is_json(value)
        {
        return toString.call(value) === '[object Object]';
        }

console.log(is_json({name: 'Robert'}));

console.log(is_json('bar'));

console.log(is_json(72));

Sample Output:

true
false
false

Flowchart:

Flowchart: JavaScript - Validate whether a given value type is pure json object or not.

Live Demo:

See the Pen javascript-validation-exercise-7 by w3resource (@w3resource) on CodePen.


For more Practice: Solve these Related Problems:

  • Write a JavaScript function that checks if a given value is a plain JSON object using Object.getPrototypeOf() and comparing it to Object.prototype.
  • Write a JavaScript function that validates whether an object was created by the Object constructor and not through custom prototypes.
  • Write a JavaScript function that distinguishes between a JSON object and other objects like arrays or Date objects.
  • Write a JavaScript function that accepts an input and returns true if it is a pure JSON object (no inherited properties besides those from Object.prototype).

Go to:


PREV : Validate Object.
NEXT : Validate RegExp.

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.