Muhammad Usman Muhammad Usman - 1 year ago 132
JSON Question

Django JSON string to json object

I am using mongodb with django and want to store json object in mongodb. Here is my code


class Data(models.Model):
deviceId = models.CharField(max_length=200)
payload = models.CharField(max_length=2000)

View save data

deviceId = request.POST.get('deviceId')
payload = request.POST.get('payload')
data = Data.objects.create(deviceId=deviceId, payload=payload);

View fetch data

data = json.dumps(list(Data.objects.all().values('deviceId','payload')))



The problem is with
. Here is a string instead of json Object.

I want
. How can convert this into json object in django. Is there anyway to convert all of the dataset into json object instead of iterating each object from dataset

Answer Source

I think you should be able to just remove the json.dumps part of the script. That's a python command to translate a valid JSON object into a string!

data = list(Data.objects.all().values('deviceId','payload'))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download