Tuesday, September 9, 2025

sai

 import csv

import json


def infer_type(value):

    """Infer DynamoDB type"""

    if value.lower() in ("true", "false"):

        return {"BOOL": value.lower() == "true"}

    try:

        int(value)

        return {"N": value}

    except ValueError:

        pass

    try:

        float(value)

        return {"N": value}

    except ValueError:

        pass

    return {"S": value}


def csv_to_dynamodb_json(csv_file, json_file):

    with open(csv_file, "r", newline="", encoding="utf-8") as f:

        reader = csv.DictReader(f)

        items = []

        for row in reader:

            item = {}

            for key, val in row.items():

                item[key] = infer_type(val)

            items.append(item)


    with open(json_file, "w", encoding="utf-8") as f:

        json.dump(items, f, indent=2)


# Example usage:

csv_to_dynamodb_json("input.csv", "output.json")