Problem creating dataset


#1

Hi everybody,
I’ve a problem to create dataset. With the following code I’ve an error: Exception: Invalid JSON was received by the server.
f = open(‘result.csv’,‘r’)
if (f):
logging.info(“opened : \n”)
reader = csv.DictReader(f)
#row=next(reader)
new_headers=[]
output_rows = []
json_model = “”
for header in reader.fieldnames:
if (header.startswith("_",0)):
if (header=="_time"):
new_headers.append(header)
else:
new_headers.append(header)
if (header==“time”):
json_model += “’”+header+"’ : { ‘type’: ‘datetime’, ‘name’: ‘Time’},"
else:
json_model += “’”+header+"’ : { ‘type’: ‘number’, ‘name’: ‘"+header +"’, ‘optional’: True},"
json_model = “{”+json_model[:-1]+"}"
print json_model
##json_model={‘time’ : { ‘type’: ‘datetime’, ‘name’: ‘Time’},‘webdirectj-7’ : { ‘type’: ‘number’, ‘name’: ‘webdirectj-7’, ‘optional’: True},‘webdirectj’ : { ‘type’: ‘number’, ‘name’: ‘webdirectj’, ‘optional’: True
}}
for row in reader:
output_rows.extend([{new_headers[i]:row[new_headers[i]] for i in range(len(new_headers))}])

### chargement dans le dataset
dataset = client.datasets.find_or_create('test_soa',json_model)

#2

I’ve found the solution. I share for interesting people.
It’s Python/Json trick.
use ast.literal_eval to format the string, then you dump, then you load:

json_model=ast.literal_eval(json_model)
d=json.dumps(json_model)
dataset = client.datasets.find_or_create('test_soa',json.loads(d))