13

I am using JSON object as an input in the textfield. Is there any way to validate this JSON object in JavaScript??

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
user1059229
  • 165
  • 1
  • 1
  • 4

5 Answers5

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
  • @user3816621 A number string is a valid JSON string. – Heretic Monkey Sep 01 '20 at 12:51
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

Run it through a JSON parser (json2.js if you aren't using a library with one built in) and see if you get data back.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Yes, there are quite a few JSON libraries available for your use.

Try these out when using Java:

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