isJsonDecodable method

bool isJsonDecodable()

Checks if the string is a valid JSON.

Example:

print('{"name": "Flutter"}'.isJsonDecodable()); // true
print("Hello".isJsonDecodable()); // false

Implementation

bool isJsonDecodable() {
  if (isEmptyOrNull) return false;
  try {
    jsonDecode(this!) as Map<String, dynamic>;
  } on FormatException {
    return false;
  }
  return true;
}