I am using JSON object as an input in the textfield. Is there any way to validate this JSON object in JavaScript??
Asked
Active
Viewed 3.9k times
13
-
Are you trying to validate that it's syntactically valid JSON or that it conforms to a certain schema? – jabclab Dec 08 '11 at 13:13
-
I am just trying to check whether the object entered in textfield is valid JSON. – user1059229 Dec 08 '11 at 13:17
-
you can use intval function for example $id=intval($id); echo json_encode($data) – rashidkaif1 Dec 26 '12 at 12:54
5 Answers
28
Building on the idea of @Quentin, you can just do something like:
function isValidJson(json) {
try {
JSON.parse(json);
return true;
} catch (e) {
return false;
}
}
console.log(isValidJson("{}")); // true
console.log(isValidJson("abc")); // false
This will require json2.js to be deployed in the page in order to ensure cross-browser support for the JSON
Object
.

jabclab
- 14,786
- 5
- 54
- 51
-
Thanks for replying. But I am getting an error for valid JSON object also. – user1059229 Dec 08 '11 at 13:51
-
{ "id":"b9c8e5f2-50ae-102c-8da0-dv8gb9f250ae", "at":2, "tmax":100, "imp": [ { "impid":"dv8gb9f2-50ae-102c-8da0-12313a002cd2", "h":50, "w":320, "instl":0 } ], "app": { "aid":"73737", "name":"We Rule", "ver":"2.1.3" }, "device": { "dpid":"dfe9f2bdfe9f2bdfe9f2bdfe9f2bdfe9f2bdfe9f2b", "ip":"124.32.53.1", "ua":"Mozilla%2F5.0%20(iPhone%3B%20U%3B%20CPU%20iPhone" } } – user1059229 Dec 09 '11 at 11:53
-
It appears to work for me, have a look at this [jsFiddle](http://jsfiddle.net/F2ufR/). – jabclab Dec 09 '11 at 11:58
-
I am using this function on "onsubmit". Should this be a problem?? – user1059229 Dec 12 '11 at 10:19
-
Shouldn't be. Is it possible to set up a [jsFiddle](http://jsfiddle.net/) to demonstrate the scenario in which the code is failing? – jabclab Dec 12 '11 at 10:24
-
It worked. I was not passing correct value to the function.Thank u for ur help. – user1059229 Dec 13 '11 at 10:19
-
Its giving me an error for the same JSON string in IE compatibility view. – user1059229 Dec 19 '11 at 11:45
-
Have you got [json2.js](https://github.com/douglascrockford/JSON-js/blob/master/json2.js) included in your page? – jabclab Dec 21 '11 at 08:45
-
It doesn't work when you put numbers as an input. For example: console.log(isValidJson("123")); // true I think we need also check if input is a number. – user3816621 Sep 01 '20 at 12:31
-
3
if you wish to validate the object to a certain schema, you could try JSD Validator

Mark Homans
- 637
- 1
- 6
- 12
0
Here is the code that worked for me,
$scope.jsonFunc = function(json){
try {
$scope.jsonData = JSON.stringify(JSON.parse(json), null, 2);
return true;
$scope.errorMessage = "Valid JSON";
} catch (e) {
$scope.errorMessage = e.message;
return false;
}
}

Anusha Bhat
- 391
- 4
- 6
0
Yes, there are quite a few JSON libraries available for your use.
Try these out when using Java:
- Jackson - A High performance JSON processors
- google-gson - Java library by google
Or if you prefer simple JavaScript, you can go with
David Walsh has given a full example of how to do this in javascript using Mootools, JSON Schema, in the following blog http://davidwalsh.name/json-validation. Give it a go.

WarFox
- 4,933
- 3
- 28
- 32