forked from FIWARE/data-models
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyValues2Normalized.py
More file actions
74 lines (49 loc) · 1.47 KB
/
Copy pathkeyValues2Normalized.py
File metadata and controls
74 lines (49 loc) · 1.47 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Converts a NGSIv2 Simplified Representation (a.k.a. keyValues)
into a Normalized Representation
Copyright (c) 2018 FIWARE Foundation e.V.
Author: José Manuel Cantera
"""
import sys
import json
import ntpath
def keyValues_2_normalized(entity):
out = {}
for key in entity:
if key == 'id' or key == 'type':
out[key] = entity[key]
continue
out[key] = {
'value': entity[key]
}
if key == 'location':
out[key]['type'] = 'geo:json'
if key.startswith('date'):
out[key]['type'] = 'DateTime'
if key == 'address':
out[key]['type'] = 'PostalAddress'
if key.startswith('ref'):
out[key]['type'] = 'Relationship'
if key.startswith('has'):
out[key]['type'] = 'Relationship'
return out
def read_json(infile):
with open(infile) as data_file:
data = json.loads(data_file.read())
return data
def write_json(data, outfile):
with open(outfile, 'w') as data_file:
json.dump(data, data_file, indent=4)
data_file.write("\n")
def main(args):
data = read_json(args[1])
result = keyValues_2_normalized(data)
file_name = ntpath.basename(args[1])
write_json(result, 'example-normalized.json')
if __name__ == '__main__':
if(len(sys.argv) != 2):
print("Usage: keyvalues2Normalized [file]")
exit(-1)
main(sys.argv)