sarish-json-extract
A lightweight utility to extract JSON from text — especially useful for parsing LLM responses that embed JSON inside prose or code fences.
Features
- Extract JSON from plain strings
- Extract JSON from
```jsonand```code fences - Extract JSON embedded in prose (e.g. LLM responses like
"Sure! Here's the data: {"name": "Sarish"} hope that helps") - Handles nested objects and arrays
- Optional schema validation (type-check extracted fields)
- Zero dependencies — pure Python standard library
- Type hints with
py.typedmarker (PEP 561)
Installation
pip install sarish-json-extract
Quick Start
from sarish_json_extract import extract_json
# Plain JSON
result = extract_json('{"name": "Sarish", "age": 30}')
print(result) # {'name': 'Sarish', 'age': 30}
# JSON in code fences
result = extract_json('''Here is the data:
```json
{"city": "Bangalore"}
''') print(result) # {'city': 'Bangalore'}
JSON embedded in LLM prose
result = extract_json('Sure! The answer is {"value": 42} as shown below.') print(result) # {'value': 42}
JSON arrays
result = extract_json('Items: [1, 2, 3] done.') print(result) # [1, 2, 3]
## Schema Validation
Pass an optional `schema` dict to validate that extracted JSON has the expected keys with the correct types:
```python
from sarish_json_extract import extract_json
text = 'The user data is: {"name": "RV", "age": 30, "active": true}'
result = extract_json(text, schema={"name": str, "age": int})
print(result) # {'name': 'RV', 'age': 30, 'active': True}
# Returns None if schema doesn't match
result = extract_json('{"name": 123}', schema={"name": str})
print(result) # None
API Reference
extract_json(text, schema=None)
| Parameter | Type | Required | Description |
|---|---|---|---|
text |
str |
Yes | A string that contains JSON somewhere in it |
schema |
dict[str, type] |
No | Optional type-checking schema |
Returns: dict | list | None — parsed JSON, or None if no valid JSON is found (or schema validation fails).
validate_schema(data, schema)
| Parameter | Type | Required | Description |
|---|---|---|---|
data |
dict |
Yes | The parsed JSON dict to validate |
schema |
dict[str, type] |
Yes | A dict mapping expected keys to their types |
Returns: bool — True if all keys exist with correct types, False otherwise.
License
MIT
Release files for sarish-json-extract 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| sarish_json_extract-0.0.2.tar.gz | 5.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sarish_json_extract-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.0 kB
Release files / sarish_json_extract-0.0.2.tar.gz
| Download URL | sarish_json_extract-0.0.2.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b3a9f6252ba5331a497b3406ed67532623918c4d82f1fe802939daf16c56d736
|
|
BLAKE2b-256 checksum How to use checksums |
01f632b61f6f60485a0b4f5e8032229a5f2ab95adc03389e43ded9383f0d4d88
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.6
|
Release files / sarish_json_extract-0.0.2-py3-none-any.whl
| Download URL | sarish_json_extract-0.0.2-py3-none-any.whl |
|---|---|
| Size | 4.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e4f92afb2d95c5cb432148c5dd35fea63a4cd42fdef6fad17a93df2ace03db4c
|
|
BLAKE2b-256 checksum How to use checksums |
eb9f98d5d2ccbad8aa2ab4335d5bc7748a0a3281269b1d2b2f960aed4d1be1d7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.6
|