How to Test If a String Is JSON or Not

How to test if a string is JSON or not?

Use JSON.parse

function isJson(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}

How to check if a string is a valid JSON string?

A comment first. The question was about not using try/catch.

If you do not mind to use it, read the answer below.
Here we just check a JSON string using a regexp, and it will work in most cases, not all cases.

Have a look around the line 450 in https://github.com/douglascrockford/JSON-js/blob/master/json2.js

There is a regexp that check for a valid JSON, something like:

if (/^[\],:{}\s]*$/.test(text.replace(/\\["\\\/bfnrtu]/g, '@').
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {

//the json is ok

}else{

//the json is not ok

}

EDIT: The new version of json2.js makes a more advanced parsing than above, but still based on a regexp replace ( from the comment of @Mrchief )

How to check if it's a string or json

The "easy" way is to try parsing and return the unparsed string on failure:

var data = localStorage[key];
try {return JSON.parse(data);}
catch(e) {return data;}

How to check if a json string is valid or not in JavaScript or Jquery?

To assure you have a valid json you must have a string first

function isJSON(str) {

if( typeof( str ) !== 'string' ) {
return false;
}
try {
JSON.parse(str);
return true;
} catch (e) {
return false;
}
}

How do I check if a string is valid JSON in Python?

You can try to do json.loads(), which will throw a ValueError if the string you pass can't be decoded as JSON.

In general, the "Pythonic" philosophy for this kind of situation is called EAFP, for Easier to Ask for Forgiveness than Permission.

Check whether the String is a Valid JSON String or not?

If you need to be sure it really is valid JSON you're going to need to parse it. A fast, simple, lightweight parser that I like is json-simple. Have a look at their examples here.

http://code.google.com/p/json-simple/wiki/DecodingExamples#Example_2_-_Faster_way:_Reuse_instance_of_JSONParser

Adapting your code I get:

JSONParser parser = new JSONParser();
ResultSet rs = preparedStatement.executeQuery();

while (rs.next()) {
for (String column : columnsList.split(",")) {
//check whether rs.getString(column) is a valid JSON String?
try{
parser.parse(rs.getString(column));
System.out.println("Valid JSON String data");
} catch (ParseException e) {
System.out.printlnn("Invalid JSON String data");
}
}
}

How to check whether a given string is valid JSON in Java

A wild idea, try parsing it and catch the exception:

import org.json.*;

public boolean isJSONValid(String test) {
try {
new JSONObject(test);
} catch (JSONException ex) {
// edited, to include @Arthur's comment
// e.g. in case JSONArray is valid as well...
try {
new JSONArray(test);
} catch (JSONException ex1) {
return false;
}
}
return true;
}

This code uses org.json JSON API implementation that is available on github, in maven and partially on Android.



Related Topics



Leave a reply



Submit