I tried to make the dtype parameter of the to_sql command pull from a JSON file I get the error "json.decoder.JSONDecodeError: Expecting value". Is there a way to do this?
Instead of the below code:
import sqlalchemy
resource_read.to_sql(tablename,engine,
schema = None,
if_exists='append',
dtype={'ID': sqlalchemy.types.VARCHAR(length=50),
'NAME': sqlalchemy.types.VARCHAR(length=255)})
I want to do something like this where the dtype dictionary parameter is a key value pair in a json file: the config json file would look like this:
{
"dtypes": {"ID": sqlalchemy.types.VARCHAR(length=50), "NAME": sqlalchemy.types.VARCHAR(length=255) }
}
Then the code is:
import sqlalchemy
config_path = os.path.join("C:\configs")
my_parser.add_argument('-c','-configname', dest='configname', help="Configname file")
args = my_parser.parse_args()
if args.configname:
with open(os.path.join(config_path, args.configname), 'r') as f:
config_params = json.load(f)
resource = os.path.join(resource_path, filename)
resource_read = pd.read_csv(resource, encoding='utf8', na_values='', dtype=str)
resource_read.to_sql(tablename,engine,schema = None,if_exists='append',dtype=config_params["dtypes"])
The issue is that the value for the dictionaries in the dtypes key is not in quotes, when I put it in quotes I don't get the JSON issue but I get a sqlalchemy error:
ValueError: The type of ID is not a SQLAlchemy type
Do I somehow convert the value to non-quotes when reading them in? If I just put in the dictionary within the script and I don't put quotes around the values I don't get an error and it runs fine. But I want it in a json file.