https://github.com/bstr1ckland/json-parser
A JSON parser built with Python
https://github.com/bstr1ckland/json-parser
parser python
Last synced: 2 days ago
JSON representation
A JSON parser built with Python
- Host: GitHub
- URL: https://github.com/bstr1ckland/json-parser
- Owner: bstr1ckland
- License: mit
- Created: 2025-04-05T19:49:19.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-06T17:21:08.000Z (about 1 year ago)
- Last Synced: 2025-07-03T22:18:41.954Z (12 months ago)
- Topics: parser, python
- Language: Python
- Homepage:
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JSON Parser
A Python-based JSON parser and scanner that reads tokenized input from `.txt` files, builds a parse tree, and outputs valid JSON or a structured error message.
## ๐ง Features
- Custom scanner to tokenize JSON-like inputs
- Recursive descent parser
- Handles objects, arrays, strings, numbers, booleans, and nulls
- Type validation for arrays (homogeneous elements)
- Detects and reports:
- Invalid tokens
- Duplicate keys in objects
- Type mismatches in arrays
- Malformed numbers and strings
- Extra trailing commas
- Outputs valid `.json` files or JSON-formatted error messages
## ๐ Project Structure
- json_scanner.py | Defines TokenType, Token, and Lexer classes
- json_parser.py | Main parser logic
- test0X.txt | Example input files
- output0X.json | Output files (created after running)
## ๐งช Input Format
Input `.txt` files contain pre-tokenized lines. Examples:
```
<{>
<:>
<,>
<:>
<}>
```
## ๐งพ Output Format
On success:
```json
{
"name": "Ben",
"age": 21
}
```
On error:
```json
{
"error": "Error type 5 at \"name\": Duplicate keys found."
}
```
## ๐ Getting Started
0. Ensure you have [python3](https://www.python.org/downloads/) installed.
1. Clone the repo
```
git clone https://github.com/your-username/json-parser.git
cd json-parser
```
2. Adjust test files to your liking in test.00.txt, test.01.txt, etc.
3. Run the parser
```bash
python3 json_parser.py
```
## โ ๏ธ Error Types
Specific Types:
1. Malformed number (starts/ends with a dot)
2. Empty string
3. Number starts with + or leading 0
4. Reserved words used as strings (e.g. "true")
5. Duplicate keys in objects
6. Mixed types in arrays
Other:
- Extra comma Trailing commas in arrays or objects
## ๐ License
MIT License