Skip to content

Instantly share code, notes, and snippets.

@khajer
Last active May 10, 2018 09:07
Show Gist options
  • Select an option

  • Save khajer/552f416669c510bd731cc87f453327c9 to your computer and use it in GitHub Desktop.

Select an option

Save khajer/552f416669c510bd731cc87f453327c9 to your computer and use it in GitHub Desktop.
json to type script file
import json
from pprint import pprint
import datetime
import os
classCol = []
def writefile(filepath, txt):
print '[write file]: '+filepath
with open(filepath, 'w') as the_file:
the_file.write(txt)
def createfile(classcolect):
folderName = './build'
if not os.path.exists(folderName): os.makedirs(folderName)
for c in classcolect:
filename = changeLower(c['class']+'.ts')
filepath = folderName+"/ "+filename
txt = strClassTxt(c)
writefile(filepath, txt)
def strClassTxt(c):
txt = ""
h = ""
txt += 'export class '+c['class'] +"{" +"\n"
for p in c["properties"].split(";"):
if p != "":
txt += '\t'+p+";"+"\n"
typeval = p.split(':')[1].replace("[", "").replace("]", "")
if typeval != "number" and typeval != "string" and typeval != "any":
h += "import { "+typeval+" } from './"+ changeLower(typeval)+"'" +'\n'
#consturctor
txt += "\n\tconstructor(obj?: any){" +'\n'
for p in c["properties"].split(";"):
if p != "":
typeval = p.split(':')[0].replace("?", "")
txt += "\t\tthis."+typeval+" = obj && obj."+typeval+" || null;\n"
txt += "" +'\n'
txt += "\t}"
#enclass
txt += "}"+"\n"
return h+txt
def changeUpper(str):
return str[0:1].upper()+str[1:]
def changeLower(str):
return str.lower()
def createClassWithData(data, className):
className = changeUpper(className)
dic = {'class': className}
dic["properties"] = ""
for k in data.keys():
v = data[k]
dic["properties"] += k+"?:"
flgArr = False
if isinstance(v, list):
flgArr = True
v = v[0]
if flgArr == True:
dic["properties"] += "["
if isinstance(v, dict):
dic["properties"] += changeUpper(k)
createClassWithData(v, k)
elif isinstance(v, basestring):
dic["properties"] += "string";
elif isinstance(v, (int, long, float, complex)) :
dic["properties"] += "number";
else:
dic["properties"] += "any";
if flgArr == True:
dic["properties"] += "]"
dic["properties"] += ";"
classCol.append(dic)
def main():
print(' >> generate class with json file : "test.json"')
with open('test.json') as f :
data = json.load(f)
createClassWithData(data,'Root')
print "-------------------"
createfile(classCol)
if __name__ == '__main__':
main()%
{
"status": "OK",
"message": "SUCCESS",
"result": {
"application": {
"applicationId": 59,
"applicationNo": "200813-1803-000091",
"applicationRunningNo": 91,
"applicationStatus": "NEW",
"applicationType": null,
"applicationYearMonth": "1803",
"approverOwner": null,
"channel": "CBS",
"companyCode": null,
"companyName": null,
"costCenter": null,
"createBy": "10006",
"createDate": 1520996957000,
"custCifNo": "000012431786",
"email": null,
"exportBy": null,
"exportDate": null,
"exportFlag": null,
"firstDecisionDate": null,
"firstDecisionText": null,
"firstSubmitDate": null,
"isActive": null,
"isDailyReport": null,
"isOwner": null,
"isRegisterSms": null,
"ktbVisaDebitRate": null,
"lastDecisionDate": null,
"lastDecisionText": null,
"localDebitCardRate": null,
"merchantAvgSaleTransaction": null,
"merchantAvgSaleVolume": null,
"merchantId": 45198,
"merchantMccGroup": null,
"merchantNameEng": "สาขาสงขลา+97378",
"merchantNameThai": "สาขาสงขลา+97378",
"merchantPhoneNo": null,
"merchantRegType": "IND",
"merchant_Tax_Ktb_Rate": null,
"merchantWhtRate": null,
"merchantZoneCode": null,
"otherRate": null,
"productType": null,
"qrMerchantCode": null,
"reSubmitDate": null,
"saleBranch": "200813",
"saleDepartmentCode": null,
"saleId": null,
"saleName": null,
"saleOwner": null,
"salePhoneNumber": null,
"salePosition": null,
"satelliteBy": null,
"satelliteDate": null,
"satelliteFlag": null,
"specialFlag": null,
"suspectListDate": null,
"suspectListFlag": null,
"suspectListNameEn": null,
"suspectListNameTh": null,
"taxId": "1959900081633",
"updateBy": "10006",
"updateDate": 1520996968000,
"use_Register_Tax": null
},
"eligible": 1,
"merchant": [
{
"merchantId": 35096,
"applicationType": "QR_PP",
"approverOwner": null,
"channel": "INTERNET",
"companyCode": null,
"companyName": null,
"costCenter": null,
"createBy": "10006",
"createDate": 1519034125000,
"custCifNo": "000012431786",
"email": null,
"exportBy": null,
"exportDate": null,
"exportFlag": null,
"firstDecisionDate": null,
"firstDecisionText": null,
"firstSubmitDate": 1520324703000,
"isActive": null,
"isDailyReport": null,
"isOwner": null,
"isRegisterSms": null,
"ktbVisaDebitRate": 0,
"lastDecisionDate": null,
"lastDecisionText": null,
"localDebitCardRate": 1,
"merchantAvgSaleTransaction": null,
"merchantAvgSaleVolume": null,
"merchantMccGroup": "12",
"merchantNameEng": "สาขาสงขลา+97378",
"merchantNameThai": "สาขาสงขลา+97378",
"merchantPhoneNo": "0896582749",
"merchantRegType": "IND",
"merchantStatus": "APPROVE",
"merchant_Tax_Ktb_Rate": 3,
"merchantWhtRate": null,
"merchantZoneCode": null,
"otherRate": 1,
"productType": null,
"qrMerchantCode": null,
"reSubmitDate": null,
"saleBranch": "200813",
"saleDepartmentCode": "200813",
"saleId": "10006",
"saleName": "Sumeree Tulathorn",
"saleOwner": "10006",
"salePhoneNumber": "075528226",
"salePosition": "SALE",
"satelliteBy": null,
"satelliteDate": null,
"satelliteFlag": null,
"specialFlag": null,
"suspectListDate": null,
"suspectListFlag": null,
"suspectListNameEn": null,
"suspectListNameTh": null,
"taxId": "1959900081633",
"updateBy": "10006",
"updateDate": 1520324706000,
"use_Register_Tax": null
},
{
"merchantId": 45197,
"applicationType": "QR_GOV",
"approverOwner": null,
"channel": "INTRANET",
"companyCode": "13B0100012",
"companyName": "สาขาสงขลา+97378",
"costCenter": null,
"createBy": "10006",
"createDate": 1520996958000,
"custCifNo": "000012431786",
"email": null,
"exportBy": null,
"exportDate": null,
"exportFlag": null,
"firstDecisionDate": null,
"firstDecisionText": null,
"firstSubmitDate": 1520997027000,
"isActive": null,
"isDailyReport": null,
"isOwner": null,
"isRegisterSms": null,
"ktbVisaDebitRate": 0,
"lastDecisionDate": null,
"lastDecisionText": null,
"localDebitCardRate": 1,
"merchantAvgSaleTransaction": null,
"merchantAvgSaleVolume": null,
"merchantMccGroup": "11",
"merchantNameEng": "สาขาสงขลา+97378",
"merchantNameThai": "สาขาสงขลา+97378",
"merchantPhoneNo": "0812312312",
"merchantRegType": "IND",
"merchantStatus": "APPROVE",
"merchant_Tax_Ktb_Rate": 3,
"merchantWhtRate": null,
"merchantZoneCode": null,
"otherRate": 2,
"productType": null,
"qrMerchantCode": "M20180314101056557",
"reSubmitDate": null,
"saleBranch": "200813",
"saleDepartmentCode": "200813",
"saleId": "10006",
"saleName": "Sumeree Tulathorn",
"saleOwner": "10006",
"salePhoneNumber": "075528226",
"salePosition": "SALE",
"satelliteBy": null,
"satelliteDate": null,
"satelliteFlag": null,
"specialFlag": null,
"suspectListDate": null,
"suspectListFlag": null,
"suspectListNameEn": null,
"suspectListNameTh": null,
"taxId": "1959900081633",
"updateBy": "1006",
"updateDate": 1520997060000,
"use_Register_Tax": null
},
{
"merchantId": 45198,
"applicationType": null,
"approverOwner": null,
"channel": "CBS",
"companyCode": null,
"companyName": null,
"costCenter": null,
"createBy": "10006",
"createDate": 1520996957000,
"custCifNo": "000012431786",
"email": null,
"exportBy": null,
"exportDate": null,
"exportFlag": null,
"firstDecisionDate": null,
"firstDecisionText": null,
"firstSubmitDate": null,
"isActive": null,
"isDailyReport": null,
"isOwner": null,
"isRegisterSms": null,
"ktbVisaDebitRate": null,
"lastDecisionDate": null,
"lastDecisionText": null,
"localDebitCardRate": null,
"merchantAvgSaleTransaction": null,
"merchantAvgSaleVolume": null,
"merchantMccGroup": null,
"merchantNameEng": "สาขาสงขลา+97378",
"merchantNameThai": "สาขาสงขลา+97378",
"merchantPhoneNo": null,
"merchantRegType": "IND",
"merchantStatus": "NEW",
"merchant_Tax_Ktb_Rate": null,
"merchantWhtRate": null,
"merchantZoneCode": null,
"otherRate": null,
"productType": null,
"qrMerchantCode": null,
"reSubmitDate": null,
"saleBranch": "200813",
"saleDepartmentCode": null,
"saleId": null,
"saleName": null,
"saleOwner": null,
"salePhoneNumber": null,
"salePosition": null,
"satelliteBy": null,
"satelliteDate": null,
"satelliteFlag": null,
"specialFlag": null,
"suspectListDate": null,
"suspectListFlag": null,
"suspectListNameEn": null,
"suspectListNameTh": null,
"taxId": "1959900081633",
"updateBy": "10006",
"updateDate": 1520996968000,
"use_Register_Tax": null
}
],
"whitelist": ""
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment