Loading Local JSON into SQL Server with Sling

Slinger avatar
Slinger
Cover for Loading Local JSON into SQL Server with Sling

Introduction

In today’s data-driven world, efficiently moving data between different systems is a critical requirement for businesses. One common scenario is the need to migrate data from local JSON files into SQL Server databases. While this might seem straightforward, traditional approaches often involve writing complex scripts, handling data type conversions, and dealing with nested JSON structures - making the process time-consuming and error-prone.

Enter Sling - a modern data movement and transformation tool designed to simplify these exact scenarios. In this comprehensive guide, we’ll walk through how Sling makes the process of migrating data from local JSON files to SQL Server both efficient and straightforward.

Installation and Setup

Getting started with Sling is straightforward. Let’s walk through the installation process and initial setup.

Installing Sling CLI

Sling provides multiple installation options to suit your operating system:

# Install using Homebrew (macOS)
brew install slingdata-io/sling/sling

# Install using curl (Linux)
curl -LO 'https://github.com/slingdata-io/sling-cli/releases/latest/download/sling_linux_amd64.tar.gz' \
  && tar xf sling_linux_amd64.tar.gz \
  && rm -f sling_linux_amd64.tar.gz \
  && chmod +x sling

# Install using Scoop (Windows)
scoop bucket add sling https://github.com/slingdata-io/scoop-sling.git
scoop install sling

# Install using Python pip
pip install sling

After installation, verify that Sling is properly installed:

# Check Sling version
sling --version

For more installation options and detailed instructions, visit the official documentation.

Setting Up Connections

For our JSON to SQL Server migration, we need to configure a SQL Server connection for writing data

SQL Server Connection

For SQL Server, you have several options to configure the connection. Here’s how to set it up using the sling conns set command:

# Set up SQL Server connection with individual parameters
sling conns set MSSQL type=sqlserver host=<host> user=<user> database=<database> password=<password> port=<port>

# Or use a connection URL
sling conns set MSSQL url="sqlserver://myuser:[email protected]:1433?database=mydatabase"

Alternatively, you can add the connection details to your ~/.sling/env.yaml file:

connections:
  MSSQL:
    type: sqlserver
    host: <host>
    user: <user>
    port: <port>
    database: <database>
    schema: <schema>
    password: <password>
    encrypt: 'true'
    trust_server_certificate: 'true'

You can also use environment variables:

# Export SQL Server connection string
export MSSQL='sqlserver://myuser:[email protected]:1433?database=mydatabase'

Testing Connections

After setting up your connections, it’s important to verify they work correctly:

# Test the local connection
sling conns test LOCAL

# Test the SQL Server connection
sling conns test MSSQL

For more details about connection configuration, refer to:

Understanding the Challenge

Before diving into the solution, let’s understand why moving data from JSON to SQL Server can be challenging:

  • Data Type Mapping: JSON and SQL Server use different data types, requiring careful mapping and conversion
  • Nested Structures: JSON files often contain nested objects and arrays, which need to be flattened or properly structured for relational storage
  • Performance: Loading large JSON files requires efficient processing and bulk loading capabilities
  • Error Handling: Dealing with malformed JSON or data type mismatches needs robust error handling
  • Schema Management: Creating and updating SQL Server tables to match JSON structures can be complex

Traditional approaches might involve writing custom scripts using Python or PowerShell, which requires significant development time and maintenance effort. Sling addresses these challenges with its purpose-built data integration capabilities.

Data Migration Approaches

Sling offers two main approaches for migrating data from JSON files to SQL Server: using CLI flags for quick operations and using YAML configurations for more complex scenarios.

Using CLI Flags

The CLI approach is perfect for quick, one-off migrations or when you want to test your data pipeline. Here’s a basic example:

# Basic JSON to SQL Server migration
sling run \
  --src-conn LOCAL \
  --src-stream "/path/to/data.json" \
  --src-options '{"flatten": true}' \
  --tgt-conn MSSQL \
  --tgt-object "my_schema.my_table"

# Migration with additional options
sling run \
  --src-conn LOCAL \
  --src-stream "/path/to/data.json" \
  --src-options '{"flatten": true, "jmespath": "data.records", "empty_as_null": true}' \
  --tgt-conn MSSQL \
  --tgt-object "my_schema.my_table" \
  --tgt-options '{"column_casing": "snake", "table_keys": {"primary": ["id"]}, "add_new_columns": true}'

Common source options for JSON files:

  • flatten: Flattens nested JSON structures
  • jmespath: Extracts specific data using JMESPath expressions
  • empty_as_null: Treats empty strings as NULL values
  • datetime_format: Specifies the format for datetime fields

Common target options for SQL Server:

  • column_casing: Controls column name casing (snake, camel, etc.)
  • table_keys: Specifies primary key columns
  • add_new_columns: Automatically adds new columns if they appear in the source
  • table_ddl: Customizes table creation SQL

Using Replication YAML

For more complex migrations or when you need to maintain the configuration in version control, use a YAML-based replication file. Create a file named replication.yaml:

source: local
target: mssql

defaults:
  source_options:
    flatten: true
    empty_as_null: true
    jmespath: "data.records"

streams:
  /path/to/customers.json:
    mode: full-refresh
    object: dbo.customers
    target_options:
      column_casing: snake
      table_keys:
        primary: [id]
      add_new_columns: true

    columns:
      id: int
      name: varchar(100)
      email: varchar(255)
      created_at: datetime
  
  /path/to/transactions.json:
    mode: full-refresh
    object: dbo.transactions

To run the replication:

# Run the replication configuration
sling run -r replication.yaml

The YAML approach offers several advantages:

  • Version control friendly
  • Support for multiple streams in one configuration
  • Detailed column specifications
  • Reusable configurations
  • Runtime variables support

For more details about replication options, refer to:

Conclusion

Migrating data from JSON files to SQL Server doesn’t have to be complicated. Sling provides a powerful yet simple solution that can be approached in multiple ways:

  1. CLI-based approach: Perfect for quick migrations and automation
  2. YAML configurations: Ideal for complex, version-controlled workflows

Key takeaways:

  • Sling automatically handles JSON parsing and SQL Server table creation
  • Multiple options for handling nested JSON structures
  • Efficient bulk loading for optimal performance
  • Flexible configuration options for different needs

Next Steps

To continue your journey with Sling:

  1. Explore the official documentation
  2. Join the Sling community on Discord
  3. Check out more examples and tutorials