Last active
December 29, 2015 01:49
-
-
Save anabarasan/7596025 to your computer and use it in GitHub Desktop.
Script to generate JSON from a csv file with FieldName and Field Type information to be used in OrangeScape Visual PaaS for creating Model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# GenerateImportJson.py | |
# generates the JSON to be used to create model using the Import Json Functionality | |
# Anbarasan <nasarabna@gmail.com> | |
# usage GenerateImportJson.py <ModelName> <path to Model params csv file> | |
import json, sys | |
modelName = sys.argv[1] | |
paramsFile = sys.argv[2] | |
# To store the generated json | |
modelJson={} | |
# change this value to get as many columns of data | |
# you want in the studio and in the default form | |
modelJson[modelName] = {"Layout":5} | |
f = file(paramsFile) | |
params = {} | |
for line in f: | |
attributes = {} | |
FieldName, FieldType = line.split(",") | |
if FieldType == 'Number': | |
attributes["Category"] = "Number" | |
attributes["Format"] = "###.##" | |
elif FieldType == 'DateTime': | |
attributes["Category"] = "Date" | |
attributes["Format"] = "dd/mm/yyyy" | |
else: | |
attributes["Category"] = "Text" | |
attributes["Format"] = "Text" | |
params[FieldName] = attributes | |
f.close() | |
modelJson[modelName]["Fields"] = params | |
# write the JSON to a file with modelName as the file name | |
out = file(modelName+'.json', 'w') | |
out.write(json.dumps(modelJson)) | |
out.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment