-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathcreate_jsonschema_from_csv.py
More file actions
76 lines (69 loc) · 2.59 KB
/
create_jsonschema_from_csv.py
File metadata and controls
76 lines (69 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
################################################################################
# Licensed to the FIWARE Foundation (FF) under one
# or more contributor license agreements. The FF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Author: [email protected]
################################################################################
# This program will create a json schema based on a csv payload submitted through a web form
# contact [email protected]
import requests
import sys
import re
def convert_csv_json(header, csv, separator, quoted):
# csv is the variable with the csv payload
# separator is , or ;
# quoted could have either, "none", "simple" or "double"
pattern = chr(10) + "|" + chr(13)
lines = re.split(pattern, csv)
# print("lines")
# print(lines)
rawHeader = header.split(separator)
if quoted == "simple":
header = [i.replace("'", "") for i in rawHeader]
elif quoted == "double":
header = [i.replace(chr(34), "") for i in rawHeader]
else:
header = rawHeader
rawValues = lines[0].split(separator)
if quoted == "simple":
values = [i.replace("'", "") for i in rawValues]
elif quoted == "double":
values = [i.replace(chr(34), "") for i in rawValues]
else:
values = rawValues
# print("header")
# print(header)
# print("values")
# print(values)
mergeValues = []
for item in values:
try:
if float(item) == item:
mergeValues.append(item)
else:
mergeValues.append(chr(34) + str(item)+ chr(34))
except:
mergeValues.append(chr(34) + str(item)+ chr(34))
mergeHeader = [chr(34) + i + chr(34) for i in header]
return dict(zip(mergeHeader, mergeValues))
header = sys.argv[1]
# print(header)
payload = sys.argv[2]
# print(payload)
email = sys.argv[3]
# print(email)
output = convert_csv_json(header, payload, ",", "double")
url = "https://smartdatamodels.org/extra/create_jsonschema_from_payload.php?jsonpayload=" + str(output) + "&mail=" + email
req = requests.get(url)
text = req.text
print(text)