I have existing Python programs which use import json
and use
json.load() json.loads()
method to read the json and present it in code in easy to use accessible dictionary.
I use python json[name][] = value
constructs to reference the json data in memory and also use the same type of syntax to assign new values to json elements.
I want to migrate the Python code to Kotlin and looking for JSON libraries which will do something close to what Python json.load() does in Kotlin.
I did use Google but did not find anything, there are plenty of Kotlin/Java JSON libraries but I dont think they offer anything like Python JSON load methods.
What is my best option library to use?
Here are Python code details, I want to do same in Kotlin and use the existing JSON libraries.
import json
js = json.loads(response['Body'].read())
Contents of js document is this:
{
"environment": "production",
"postgres_host" : "pgstaging-prod.blah.blah.rds.amazonaws.com",
"postgres_port" : 5432,
"postgres_database" : "datawarehouse",
"postgres_user" : "pguser",
"postgres_password" : "!!!!!!",
"postgres_config_schema" : "etl_operations",
"postgres_validation_log_table_name" : "jobs_process_log",
"postgres_destination_table_name" : "myleads",
"debugFlag": true,
"configBucket": "csn-datalake",
"configFileName": "yurib_test/myleads.json",
"HighWaterMarkFileName": "yurib_test/dts_high_watermark.json",
"repartitionSourceTableName": "leads",
"repartitionSourceDbName": "datalake",
"sourceTablePartitionDateTime": "job_run_timestamp_utc",
"repartitionTargetTableName": "outleads",
"repartitionTargetDbName": "datalake_reports",
"repartitionTargetS3Location": "yurib_test/outleads",
"repartitionTargetColumnList": [
{"colName": "message_item_countrycode", "isDatePartition": false, "renameTo" : "message_countrycode"},
{"colName": "message_tenant_code", "isDatePartition": false, "renameTo" : "message_tenant"},
{"colName": "message_lastupdated", "isDatePartition": true, "renameTo" : "message_lastupdated"}
],
"repartitionLoadType": "incremental_data_load",
"repartition": 4,
"repartitionLoadTypeIncremental": "incremental_data_load",
"repartitionLoadTypeFullInitial": "full_data_load",
"countryCodeColName": "message_item_countrycode",
"tenantColName": "message_tenant_code",
"autoCreateCountryTenant": false,
"autoCreateCountry": true,
"autoCreateTenant": true,
"partitionDateDefault": "0000-00-00",
"partitionYearDefault": "0000",
"partitionMonthDefault": "00",
"partitionDayDefault": "00",
"countryCodeDefault": "AU",
"tenantDefault": "CARSALES",
"missingPartColDataValReplace": "MISSINGDATA",
"validateIncomingCountryTenant": true,
"datePartitionStyle": "ym",
"datePartitionStyleYearMonth": "ym",
"datePartitionStyleYearMonthDay": "ymd",
"propagateGlueJobRunGuid": false
}
here is how Python can access above json document using [] and range
print (js["repartitionLoadType"])
print (js['configBucket'], js['configFileName'], js['HighWaterMarkFileName'])
print (js['repartitionTargetTableName'], js['repartitionTargetS3Location'])
print (js['repartitionSourceTableName'], js['repartitionSourceDbName'])
print (js['repartitionTargetDbName'], js['repartitionLoadType'])
print (js['autoCreateCountry'], js['autoCreateTenant'], js['missingPartColDataValReplace'])
print (js['countryCodeColName'], js['tenantColName'], js['propagateGlueJobRunGuid'])
print (js['countryCodeDefault'], js['tenantDefault'], js['validateIncomingCountryTenant'], js['repartition'])
partition_dts = ""
# json array
for i in range(0, len(js['repartitionTargetColumnList'])):
if True == js['repartitionTargetColumnList'][i]['isDatePartition']:
partition_dts = "`" + js['repartitionTargetColumnList'][i]['colName'] + "`"
else:
js['repartitionTargetColumnList'][i]['colName'] = "new value replace/assign here"
continue
# to set/replace/assign any values above:
js["repartitionLoadType"] = "some new value"
I hope this clarifies what I am trying to do to migrate my Python code to Kotlin.