Skip to main content

Package to convert JSON data in SQL table.

Project description

Maceió

Package to insert JSON data into a SQL database.

What it does

In short, given a JSON input, Maceio is able to:

  • Create, and populate, a new table based on a JSON input (no need to specify its structure)
  • Feed JSON records into an existing table (inserting a new register or updating existing ones)

Getting Started

1. No structure, no db options (quick option)

from Maceio.Maceio import Maceio

if __name__ == "__main__":
    # You can use only one dictionary without lists
    data = '''[
            {
                "field1":{
                    "subfield1": 1,
                    "subfield2": "TYPE1",
                    "subfield3":{
                        "sub1":"Value 1",
                        "sub2":"Value 2",
                    }
                }
            },
            {
                "field1":{
                    "subfield1": 2,
                    "subfield2": "TYPE1",
                    "subfield3":{
                        "sub1":"Value 3",
                        "sub2":"Value 4",
                    }
                }
            }
        ]
    ''')

    maceio = Maceio('postgresql+psycopg2://user:password@host:port/database', 'schema_example')
    maceio.save('table_example', data, conflicts=('subfield1', 'subfield2'))

2. Adding db options (connection limits and pool size)

from Maceio.Maceio import Maceio

if __name__ == "__main__":
    # You can use only one dictionary without lists
    data = '''[
            {
                "field1":{
                    "subfield1": 1,
                    "subfield2": "TYPE1",
                    "subfield3":{
                        "sub1":"Value 1",
                        "sub2":"Value 2",
                    }
                }
            },
            {
                "field1":{
                    "subfield1": 2,
                    "subfield2": "TYPE1",
                    "subfield3":{
                        "sub1":"Value 3",
                        "sub2":"Value 4",
                    }
                }
            }
        ]
    ''')
    maceio = Maceio('postgresql+psycopg2://user:password@host:port/database', 'schema_example', pool_size=5, max_overflow=2)
    maceio.save('table_example', data, conflicts=('subfield1', 'subfield2'))

3. Disabling table existence check

from Maceio.Maceio import Maceio

if __name__ == "__main__":
    # You can use only one dictionary without lists
    data = '''[
            {
                "field1":{
                    "subfield1": 1,
                    "subfield2": "TYPE1",
                    "subfield3":{
                        "sub1":"Value 1",
                        "sub2":"Value 2",
                    }
                }
            },
            {
                "field1":{
                    "subfield1": 2,
                    "subfield2": "TYPE1",
                    "subfield3":{
                        "sub1":"Value 3",
                        "sub2":"Value 4",
                    }
                }
            }
        ]
    ''')
    maceio = Maceio('postgresql+psycopg2://user:password@host:port/database', 'schema_example', pool_size=5, max_overflow=2)

    # To not check if table exists, set verify=False
    maceio.save('table_example', data, conflicts=('subfield1', 'subfield2'), verify=False, primaries=('id',))

Disabling JSON format

from Maceio.Maceio import Maceio

if __name__ == "__main__":
    # You can use only one dictionary without lists
    data = '''[
            {
                "field1":{
                    "subfield1": 1,
                    "subfield2": "TYPE1",
                    "subfield3":{
                        "sub1":"Value 1",
                        "sub2":"Value 2",
                    }
                }
            },
            {
                "field1":{
                    "subfield1": 2,
                    "subfield2": "TYPE1",
                    "subfield3":{
                        "sub1":"Value 3",
                        "sub2":"Value 4",
                    }
                }
            }
        ]
    ''')

    # Passing enableJsonType=False disables storing with JSON type, so JSON data will be saved as string instead
    maceio = Maceio('postgresql+psycopg2://user:password@host:port/database', 'schema_example', pool_size=5, max_overflow=2, enableJsonType=False)
    maceio.save('table_example', data, conflicts=('subfield1', 'subfield2'), verify=True, primaries=('id',))

Saving all data types as string instead

This can be particularly useful for initial exploration of data with unknown data types.

from Maceio.Maceio import Maceio

if __name__ == "__main__":
    # You can use only one dictionary without lists
    data = '''[
            {
                "field1":{
                    "subfield1": 1,
                    "subfield2": "TYPE1",
                    "subfield3":{
                        "sub1":"Value 1",
                        "sub2":"Value 2",
                    }
                }
            },
            {
                "field1":{
                    "subfield1": 2,
                    "subfield2": "TYPE1",
                    "subfield3":{
                        "sub1":"Value 3",
                        "sub2":"Value 4",
                    }
                }
            }
        ]
    ''')

    # To save all formats as string, pass onlyText=True
    maceio = Maceio('postgresql+psycopg2://user:password@host:port/database', 'schema_example', pool_size=5, max_overflow=2, onlyText=True)
    maceio.save('table_example', data, conflicts=('subfield1', 'subfield2'), verify=True, primaries=('id',))

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

Maceio-1.0.6.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Maceio-1.0.6-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file Maceio-1.0.6.tar.gz.

File metadata

  • Download URL: Maceio-1.0.6.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.10

File hashes

Hashes for Maceio-1.0.6.tar.gz
Algorithm Hash digest
SHA256 0e19e27f2902ade42faba69c016b6b92f928ba76c20a3765ee3fef7cae0fccfe
MD5 8f5ea326ed3f4d2d229b5cec10c17241
BLAKE2b-256 edb1831a31ce080a726b098890f046d2fbd49f256252aba7afa7072c0b247b6f

See more details on using hashes here.

File details

Details for the file Maceio-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: Maceio-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.10

File hashes

Hashes for Maceio-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 99050c95aa78271700e8cde8c5ef2b08aa705efcbeef690d2af27f69334f3cf6
MD5 80056c1cf982b385ab4c13d082d23411
BLAKE2b-256 c60a3d11c3d17bac4543bf58103a204e1247161ece292357eaded005cfd542c3

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