Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
|badge1| |badge2| |badge3|
.. |badge1| image:: https://github.com/robabram/python-easy-json/actions/workflows/tests.yaml/badge.svg :alt: Unittest Completion Status .. |badge2| image:: https://raw.githubusercontent.com/robabram/python-easy-json/coverage-badge/coverage.svg?raw=true :alt: Code Coverage Status .. |badge3| image:: https://img.shields.io/badge/python-v3.7%20|%20v3.8%20|%20v3.9%20|%20v3.10%20|%20v3.11%20|%20v3.12-blue :alt: Python v3.7, v3.8, v3.9, v3.10, v3.11, v3.12
python-easy-json: simple, yet powerful, JSON/python dictionary to object deserialization
python-easy-json is a recursive JSON to python object deserializer with support for defining data models and casting data to python using type hint annotations.
The python-easy-json JSONObject class can be used to:
$ pip install python-easy-json
After years of python development, I grew tired of receiving data from APIs, database, csv files and so on and working with them as python dictionaries. The "simple" JSON deserializer library options I saw really didn't fit how easy I felt that deserializing to a JSON object should be. Additionally, I wanted to create simple data model classes and using python "Type Hinting" to define property value types.
Just pass a JSON string or python dict argument to the JSONObject constructor. In this example, we can switch from using dict key lookups to an array of JSONObjects.
::
for row in results:
if row['the_key'][0]['another_key'] == 'the_value':
...
With JSONObject this may be re-written as below, using list comprehension. This makes the code more readable and less cluttered when working with complex dictionary structures in code.
::
from python_easy_json import JSONObject
for row in [JSONObject(r) for r in results]:
if row.the_key[0].another_key == 'the_value':
...
Data from a JSON String
::
from python_easy_json import JSONObject
# JSON string
obj = JSONObject('{"test_key": "test_value"}')
print(obj.to_json())
{"test_key": "test_value"}
Data from a python dictionary
::
# Python dictionary
obj = JSONObject({'test_key': 'test_value'})
print(obj.to_json())
{"test_key": "test_value"}
Using the python-easy-json JSONObject class, you can create data models, including deeply nested models and arrays, from any JSON string or dictionary. Additionally, python "Type Hints" may be used to cast values to the type defined by the type hint annotations.
As a bonus; IDEs with auto-completion and support for python type hinting will auto-complete model properties as you type.
This example shows how to define nested/child data models, including lists of nested data models.
::
# Represents json from 'tests/test_data/nested_data_1.json'
class CakeToppingTypeModel(JSONObject):
id: int = None
type: str = None
class CakeBatterTypeModel(JSONObject):
id: int = None
type: str = None
class CakeBatterModel(JSONObject):
batter: List[CakeBatterTypeModel] = None
class CakeModel(JSONObject):
id: str = None
type: str = None
name: str = None
ppu: float = None
batters: CakeBatterModel = None
topping: List[CakeToppingTypeModel]
cake = CakeModel(data)
print(f'Cake: {cake.name} ({len(cake.batters.batter)} ingredents).')
Cake: Devil's Food Cake (4 ingredients).
If a model has been defined and the properties have python Type Hint annotations, the JSONObject can convert values to the annotation types.
::
from datetime import datetime
class TimestampModel(JSONObject):
id: int = None
timestamp: datetime = None
data = {'id': "123", "timestamp": "2022-09-19 10:11:01.123456"}
obj = TimestampModel(data, cast_types=True)
if obj.id > 0:
print(f"ID: {obj.id}: {obj.timestamp.strftime('%b %d, %Y @ %H:%M:%S %p')}")
$ ID: 123: Sep 19, 2022 @ 10:11:01 AM
JSONObject Class
::
JSONObject.__init__(data: Union[Dict, str, None] = None, cast_types: bool = False, ordered: bool = False)
Load the dictionary or JSON string data argument into ourselves as properties.
:param data: Dictionary or valid JSON string.
:param cast_types: If properties of this class are type annotated, try to cast them.
:param ordered: Use OrderedDict() if set, otherwise use dict(). For python <= 3.6.
JSONObject.to_json(indent: int = None)
Export stored data as a json string.
:param indent: Positive integer value for formatting JSON string indenting.
:returns: JSON string
JSONObject.to_dict(recursive: bool = True, dates_to_str: bool = False)
Export stored data as a python dictionary object.
:param recursive: Boolean, recursively convert nested JSONObjects to a dict
:param dates_to_str: Boolean, convert all date or datetime values to string.
:returns: dictionary object
JSONObject.update([Dict|List|Tuple]) accepts either a dictionary object or an iterable of key/value
pairs (as tuples or other iterables of length two). If keyword arguments are specified, the dictionary
is then updated with those key/value pairs: obj.update(sky=1, cloud=2).
Plus Operator: Two JSONObjects may be merged using the plus (+) operator: obj = obj + other_obj.
Number of Properties: The number of managed properties may be determined by using the Python 'len()'
function: len(obj) == 5.
MIT licensed. See the bundled LICENSE <https://github.com/robabram/python-easy-json/blob/main/LICENSE>
file for more details.
Testing JSON data for examples and unittests sourced from: https://opensource.adobe.com/Spry/samples/data_region/JSONDataSetSample.html
FAQs
A simple, yet powerful, JSON/python dictionary to object deserialization
We found that python-easy-json demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.