I’m working on a project for a Intro to programming logic class for school where we are reading in information from a .txt file, reformatitng it to JSON format and then writing that JSON format to a new .txt file while also printing the JSON formatted data to the screen.
I’m having an issue where my JSON key value pairs are putting quotes on data types that do not need quotes, such as integers and booleans.
def main():
runtimeManager = RuntimeManager()
jsonResult = runtimeManager.run()
outputFileName=runtimeManager.outputFilename
print(jsonResult)
print("nOutput written to " +outputFileName+ ".")
class RuntimeManager:
def run(self):
## Gather required member variables
self.getUserInputFileName()
self.getUserOutputFileName()
##Grabbing user file and extracting the CSV
with open(self.inputFilename) as inputFile:
jsonResult = CsvToJson(inputFile.readlines()).transformCsvToJson()
with open(self.outputFilename, "w") as outputFile:
outputFile.write(jsonResult)
return jsonResult
def getUserInputFileName(self):
## Prompt User for
self.inputFilename = input("Enter the filename: ").strip()
def getUserOutputFileName(self):
self.outputFilename = input("Enter the OUTPUT filename: ").strip()
class CsvToJson():
def __init__(self, csv):
self.csv = csv
self.headers = csv[0].strip()
self.data = csv[1:]
def transformCsvToJson(self):
jsonResult = "{"
for i in range(0, len(self.data)):
if (i != 0):
jsonResult = jsonResult + "n,"
jsonResult = jsonResult + """ + str(i+1) + "":" + self.createJsonObject(self.headers, self.data[i])
return jsonResult + "n}"
def createJsonObject(self, headers, csvRow):
csvKeyArray = headers.split(",")
csvValueArray = csvRow.strip().split(",")
jsonObject = "{"
for i in range(0, len(csvKeyArray)):
jsonField = ""
if (i != 0):
jsonField = ","
jsonField = jsonField + """ + str(csvKeyArray[i]) + "":" + self.getValueDataType(csvValueArray[i])
jsonObject = jsonObject + jsonField
return jsonObject + "}"
def getValueDataType(self, value):
if (type(value) == int):
value = str(value)
elif (type(value) == str):
value = """ + value + """
elif (type(value) == bool):
if (value):
value = "true"
else:
value = "false"
elif (type(value) == null):
value = "null"
else:
value = "ERROR"
return value
if (__name__ == "__main__"):
main()
I’ve put my program in here with line numbers. The stipulations of my assignment dictate that we are not permitted to use the JSON module built into python.
the issue I’m having is my JSON formatting looks like this:
{"1":{"Last Name":"Minion","First Name":"Bob","Preferred Name":"Bob","Preferred Pronoun":"he/him","SF ID #":"2211-4456","Program Code":"3504","Phone":"910-432-8765","Email":"[email protected]"}
and it should look like this:
{"1":{"Last Name":"Minion","First Name":"Bob","Preferred Name":"Bob","Preferred Pronoun":"he/him","SF ID #":"2211-4456","Program Code":3504,"Phone":"910-432-8765","Email":"[email protected]"}
with the quotes not being on the code in the Program Code:3504 key value pair, and I can’t figure out why the quotes are being placed on it.
example input file content looks like this:
Last Name,First Name,Preferred Name,Preferred Pronoun,SF ID #,Program Code,Phone,Email
Minion,Bob,Bob,he/him,2211-4456,3504,910-432-8765,[email protected]
Nichols,James,James,,1234-5678,3651,352-395-5220,[email protected]
Person,Punny,,,9999-1111,3840,352-123-4567,[email protected]
Person,Punny2,,,9999-1112,3841,352-123-6792,[email protected]
Person,Punny3,,,9999-1113,3842,352-123-3420,[email protected]
Smart,Maxwell,Max,,9922-4029,3650,310-2910,[email protected]
Tree,Groot,Groot,,1122-3344,3624,361-1234-5678,[email protected]
2
Answers
Here is an example how you can produce valid json from the input you’ve provided (but beware: there are many caveats and shortcuts, without using
csv
/json
modules it will be hard to cover all cases and inputs):Prints:
All values from a split CSV line are type
str
which is why you get quotes around everything. You could write acoerce
function that tests if the string can be converted toint
:I’ll leave it as an exercise to coerce
bool
andnull
.