Extract JSON from LLM responses and mixed text
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sarish_json_extract-0.0.2.tar.gz.
File metadata
- Download URL: sarish_json_extract-0.0.2.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b3a9f6252ba5331a497b3406ed67532623918c4d82f1fe802939daf16c56d736
|
|
| MD5 |
ecdb1558acd1491096dee90bd5d0e0f5
|
|
| BLAKE2b-256 |
01f632b61f6f60485a0b4f5e8032229a5f2ab95adc03389e43ded9383f0d4d88
|
File details
Details for the file sarish_json_extract-0.0.2-py3-none-any.whl.
File metadata
- Download URL: sarish_json_extract-0.0.2-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e4f92afb2d95c5cb432148c5dd35fea63a4cd42fdef6fad17a93df2ace03db4c
|
|
| MD5 |
68490811dc5af03e8fee7b1a767d5c00
|
|
| BLAKE2b-256 |
eb9f98d5d2ccbad8aa2ab4335d5bc7748a0a3281269b1d2b2f960aed4d1be1d7
|