Skip to content

Convert JSON/dict to python object and viceversa

License

Notifications You must be signed in to change notification settings

Wiston999/json2py

Repository files navigation

json2py

Description

Convert JSON/dict to python object and vice versa

Installation

pip install json2py

Example

Using json2py makes treating with Python dicts as simple as:

    from json2py.models import *
    # Define basic object map
    class Example(NestedField):
        hello = TextField(name = 'hi')
        integer = IntegerField()
        floating = FloatField()

    # Define list map
    class ExampleList(ListField):
        __model__ = Example

    # Sample variables
    dict_var = {'hi': 'world', 'integer': 1000, 'floating': 10.5, 'ignored': "you won't see me"}
    list_var = [dict_var] * 3

    # Do the magic
    myMappedList = ExampleList(list_var)

    # Modify integer field of second list element
    myMappedList[1].integer.value = 1234

    # Show the results
    print myMappedList.json_encode(indent = 4)

Output:

    [
        {
            "integer": 1000,
            "floating": 10.5,
            "hi": "world"
        },
        {
        // Notice how "integer" value has changed
            "integer": 1234,
            "floating": 10.5,
            "hi": "world"
        },
        {
            "integer": 1000,
            "floating": 10.5,
            "hi": "world"
        }
    ]

Please, refer to Documentation examples for more examples.

Build Status

Build Status

Documentation

Documentation Status

Issues status

Stories in Ready