Skip to main content

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
    },
    ...
}

Metadata

Release files for restruct 0.2.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for restruct 0.2.7
File Size Uploaded Provenance Actions
restruct-0.2.7.tar.gz 4.4 kB Without provenance Details for restruct-0.2.7.tar.gz

Release files / restruct-0.2.7.tar.gz

Download URL restruct-0.2.7.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c1d92a11d55064670e149bffcab9a3d6bb868cde1127183e6b63590f5c642e57
BLAKE2b-256 checksum
How to use checksums
ef550cfcc514f9f3acdb5bc152f1b2b1b183d0a458634997b04e9c58959d5e82
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for restruct 0.2.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page