Skip to main content

Turn SQL results into beautiful JSON structures

Project description

Restruct is an easy light-weight formatting library that simplifies the process of representing SQL results in a human readable format (JSON). Pass the restruct method your SQL results and a formatting structure and voila, you have turned your complex SQL result into beautiful JSON. One of the best features of Restruct is squashing all the duplicate entries from SQL joins into a sub section of your JSON structure.

Usage examples

Lets assume we have a database with a user table that can be fetched with this query:

SELECT id,
       first_name,
       last_name,
       email,
       age
  FROM users

Formatting

Here is how you could write a simple template to turn the SQL result (list of dicts) into a prettier JSON structure:

struct = {
    "< id": {
        "id": "< id",
        "name": {
            "first": "< first_name",
            "last": "< last_name",
            "full": "{{first_name}} {{last_name}}"
        },
        "email": "< email",
        "age": "< age"
    }
}

collection                = Restruct.collection( <list_of_dicts_from_database_query> )
collection.format(struct)

# Example output:
{
    273667: {
        "id": 273667,
        "name": {
            "first": "Jeff",
            "last": "Goldbloom",
            "full": "Jeff Goldbloom"
        },
        "email": "jeff.goldbloom@example.com",
        "age": 49
    },
    93892: {
        "id": 93892,
        "name": {
            "first": "Marty",
            "last": "Mcfly",
            "full": "Marty Mcfly"
        },
        "email": "marty.mcfly@example.com",
        "age": 17
    },
    ...
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for restruct, version 0.2.7
Filename, size File type Python version Upload date Hashes
Filename, size restruct-0.2.7.tar.gz (4.4 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page