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.

Source Distribution

restruct-0.2.6.tar.gz (4.4 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page