This is a Python based JSON parser that validates JSON objects. It checks for the correctness of the JSON format and handles multiple types of JSON structures, including strings, numbers, booleans, null values, arrays, and nested objects.
This project helps you learn the basics of creating a simple JSON parser. The project consists of multiple steps:
- Step 1: Parsing an empty JSON object
{}
. - Step 2: Parsing a simple key-value JSON object.
- Step 3: Parsing JSON with multiple types like strings, numbers, booleans, and null values.
- Step 4: Parsing nested objects and arrays.
- Step 5: Adding custom tests.
The parser uses Python’s built-in json
module to validate JSON structures.
- Supports basic JSON object validation
- Handles JSON types: strings, numbers, booleans, nulls
- Supports nested objects and arrays
- Provides useful error messages for invalid JSON
-
Clone the repository:
git clone https://github.com/iPrakharV/JSON-Parser-Python.git cd JSON-Parser
-
Install Python: Ensure that Python 3 is installed on your machine. You can download Python here.
-
Run the parser: The main script takes a JSON object as input and determines whether it's valid or not.
python Main.py
After running the script, you'll be prompted to enter a JSON string, for example:
Enter JSON data: {"key": "value"}
-
Output: The program will print either:
Valid JSON Object
Invalid JSON Object
(with an error message if applicable)
{
"key1": true,
"key2": false,
"key3": null,
"key4": "value",
"key5": 101
}
This would result in:
Valid JSON Object
{
"key1": true,
"key2": False, # Capitalized False is invalid in JSON
"key3": null
}
This would result in:
Invalid JSON Object: Expecting value: line 3 column 10 (char 24)
You can run the pre-defined test cases stored in the tests/ folder. Each folder contains JSON files to test the parser against.
# Run tests for step 1
python parser.py < tests/step1/test1.json
# Run tests for step 2
python parser.py < tests/step2/test1.json