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.7.tar.gz (4.4 kB view details)

Uploaded Source

File details

Details for the file restruct-0.2.7.tar.gz.

File metadata

  • Download URL: restruct-0.2.7.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for restruct-0.2.7.tar.gz
Algorithm Hash digest
SHA256 c1d92a11d55064670e149bffcab9a3d6bb868cde1127183e6b63590f5c642e57
MD5 bea130e28fae1042b6eead34922b0133
BLAKE2b-256 ef550cfcc514f9f3acdb5bc152f1b2b1b183d0a458634997b04e9c58959d5e82

See more details on using hashes here.

Supported by

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