How do I check if a string is valid JSON in Python?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The reliable way to check whether a Python string contains valid JSON is to parse it. JSON validity is defined by syntax, not by regular expressions or quick string checks, so a parser is the only trustworthy answer. In Python, the standard json module already gives you everything you need.
Parse the String with json.loads
json.loads takes a string and converts it into normal Python objects. If the string is valid JSON, you get back a dict, list, str, int, float, bool, or None. If the string is invalid, Python raises json.JSONDecodeError.
Output:
That means "is this valid JSON?" is really "can json.loads parse it without throwing an exception?"
A Reusable Validation Helper
In real code, you usually want a small helper rather than scattering try and except blocks everywhere.
This prints:
This pattern is straightforward and fast enough for most application code. It also keeps the validation rule aligned with the parser you will probably use later anyway.
Returning the Parsed Value Too
Often, a pure boolean is not enough. You validate the string and then immediately need the parsed result. In that case, returning both status and data avoids parsing the same string twice.
This style is useful in CLI tools, web request handlers, and data import jobs where you want a clean control flow.
Valid JSON Does Not Always Mean "Object"
Many developers expect valid JSON to always be an object with key and value pairs. That is incorrect. A JSON string can also be an array, a number, a string literal, true, false, or null.
If your application specifically requires a JSON object, validate the syntax first and then validate the resulting type.
That distinction matters when writing API clients or config loaders. "Valid JSON" and "JSON of the shape I expect" are separate checks.
Capturing Useful Error Details
When the input is invalid, JSONDecodeError includes location information. That is valuable for debugging logs, user feedback, and test failures.
This makes it much easier to explain what went wrong than just returning False.
Common Pitfalls
One common mistake is using string heuristics such as "starts with [ or "". Those checks only guess the structure; they do not confirm valid syntax.
Another mistake is catching ValueError broadly. While JSONDecodeError inherits from ValueError, catching the more specific exception makes intent clearer and avoids hiding unrelated errors in nearby code.
A third issue is confusing Python literals with JSON literals. JSON uses lowercase true, false, and null. A string containing True or None is not valid JSON even though those are valid Python values.
Finally, be careful with already-parsed data. json.loads expects a string, bytes, or bytearray. If you already have a Python dict, validating it with loads is the wrong operation.
Summary
- Use
json.loadsto validate JSON because parsing is the real definition of validity. - Catch
json.JSONDecodeErrorto handle invalid input cleanly. - Return parsed data along with status when you want to avoid decoding twice.
- Remember that valid JSON can be a scalar, array, or object.
- Separate syntax validation from schema or type validation when your application expects a specific shape.

