I would like to write JSON Output into my python models.
My Model:
class Thing(models.Model):
name = models.CharField(max_length=50)
serial = models.BigIntegerField()
san = models.CharField(max_length=50)
validfrom = models.CharField(max_length=50)
validto = models.CharField(max_length=50)
requester = models.CharField(max_length=50)
def __unicode__(self):
return self.name
This is my JSON Output:
{
"Serial Number": "123132213",
"SAN": "hfdhfg",
"Valid From": " 04.08.2015 18:43",
"Valid To": " 03.08.2020 18:43",
"URL": "https://url.com/3212"
}
I'm executing a command that gives me this output which I try to save into my model.
# Execute command & read
stdin,stdout,stderr=ssh.exec_command(cmd)
outlines=stdout.readlines()
resp=''.join(outlines)
print(resp)
# Load JSON Output and save into model
jsonToPython = json.loads(resp)
thing_ser.validated_data['serial'] = jsonToPython['Serial Number']
thing_ser.validated_data['san'] = jsonToPython['Serial Number']
thing_ser.validated_data['validfrom'] = jsonToPython['Valid From']
thing_ser.validated_data['validto'] = jsonToPython['Valid To']
thing_ser.save()
I think this doesn't work because my JSON is not a string and json.loads excepts a string, so I'm not sure what's the right approach to this.