Muhammad Usman Muhammad Usman - 3 months ago 32
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


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'))