https://github.com/emman27/json_object
JSON Marshalling for Python
https://github.com/emman27/json_object
json marshalling python strict-types unmarshal
Last synced: 5 months ago
JSON representation
JSON Marshalling for Python
- Host: GitHub
- URL: https://github.com/emman27/json_object
- Owner: emman27
- Created: 2017-10-27T05:50:32.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2025-12-12T09:17:28.000Z (6 months ago)
- Last Synced: 2025-12-13T20:30:55.201Z (6 months ago)
- Topics: json, marshalling, python, strict-types, unmarshal
- Language: Python
- Size: 41 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 23
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# JSON Object Strict Typing [](https://circleci.com/gh/emman27/json_object) [](https://snyk.io/test/github/emman27/json_object) [](https://www.codacy.com/app/eygohlolz/json_object?utm_source=github.com&utm_medium=referral&utm_content=emman27/json_object&utm_campaign=Badge_Grade)
### Summary
Provides json unmarshalling to strict types for Python. This ensures you always get safe dictionaries for usage. This allows you to always do the below without worrying about KeyErrors.
```
schema = JSONObject(...)
json_parsed = schema.loads(json_unparsed)
json_parsed['my_key']['my_next_key']
```
Currently supports basic Python primitives: `str`, `list`, `dict`, `float`, `int`, `bool`
Coming soon: Python DateTime support
### Usage
First, start by declaring your schema of objects you expect to get. For example, if I expect to get a user representation,
```
user_schema = JSONObject({
username: str,
id: int,
name: str,
active: bool,
permissions: [
{
'resource': 'string',
'action': 'string',
}
]
})
# Make the relevant API call...
req = requests.get('...')
data = user_schema.loads(req.json()) # The loads signature is indifferent between json.loads(s) and s itself.
# You can now do things like
for perm in data['permissions']:
print(perm['resource'])
# safely without worrying about KeyErrors
```
### Example
```
from json_obj import JSONObject
>>> schema = {
'a': str,
'b': int,
'c': float,
'd': boolean,
}
>>> loader = JSONObject(schema)
>>> loader.loads({
'a': 34,
'b': True,
})
{
'a': '34',
'b': 1,
'c': 0.0,
'd': False,
}
```