DJango: formatting json serialization

I have the following kind of DJango

def company(request): company_list = Company.objects.all() output = serializers.serialize('json', company_list, fields=('name','phonenumber','email','companylogo')) return HttpResponse(output, content_type="application/json") 

The result is as follows:

 [{"pk": 1, "model": "test.company", "fields": {"companylogo": null, "phonenumber": "741.999.5554", "name": "Remax", "email": " home@remax.co.il "}}, {"pk": 4, "model": "test.company", "fields": {"companylogo": null, "phonenumber": "641-7778889", "name": "remixa", "email": " a@aa.com "}}, {"pk": 2, "model": "test.company", "fields": {"companylogo": null, "phonenumber": "658-2233444", "name": "remix", "email": " b@bbb.com "}}, {"pk": 7, "model": "test.company", "fields": {"companylogo": null, "phonenumber": "996-7778880", "name": "remix", "email": " a@aba.com "}}] 

my questions: 1. Can I control the order of the fields 2. Can I change the name of the fields 3. I expected to see the indented result in the browser, i.e. Instead of one long line to see something like this:

 [ { "pk": 1, "model": "test.company", "fields": { "companylogo": null, "phonenumber": "741.999.5554", "name": "Remax", "email": " home@remax.co.il " } }, { "pk": 4, "model": "test.company", "fields": { "companylogo": null, "phonenumber": "641-7778889", "name": "remixa", "email": " a@aa.com " } }, .... } 

]

+7
json python django
source share
4 answers

Python (unrelated to Django and since version 2.6) has a built-in json that can do the required indentation. If you are looking for something quick and dirty for debugging, you can do something like this:

 from django.http import HttpResponse from django.core import serializers import json def company(request, pretty=False): company_list = Company.objects.all() output = serializers.serialize('json', company_list, fields=('name','phonenumber','email','companylogo')) if pretty: output = json.dumps(json.loads(output), indent=4)) return HttpResponse(output, content_type="application/json") 

But this is a performance issue if the Company model is large. I recommend taking Dan R's advice and using a browser plugin to parse and render json or create another client solution. I have a script that takes a json file and does the same as the code above, reads in json and prints it with indent=4 .

As for sorting your output, you can simply use the order_by method for your set request:

 def company(request): company_list = Company.objects.order_by("sorting_field") ... 

And if you always want this model to be sorted this way, you can use the ordering metaclass:

 class Company(models.Model): class Meta: ordering = ["sorting_field"] ... 

In conclusion, if you intend to expose your models with a web service, I highly recommend taking a look at tastypie . It can help you in the long run, as it provides many other handy features that help with this.

+7
source share

you can get a beautiful format as follows:

 return JsonResponse(your_json_dict, json_dumps_params={'indent': 2}) 
+4
source share

With Django 1.7, I can get excellent JSON indentation using the indent parameter of the serializer. For example, in a command that uploads data from my database:

 self.stdout.write(serializers.serialize("json", records, indent=2)) 

The indent parameter has been in Django since version 1.5. The result I get is as follows:

 [ { "fields": { "type": "something", "word": "something else", }, "model": "whatever", "pk": 887060 }, { "fields": { "type": "something more", "word": "and another thing", }, "model": "whatever", "pk": 887061 }, ... 

To order your records, you will need to do what Kevin suggested and use order_by , or any other method you want to order for the records that you pass to the serializer. For example, I use itertools.chain to order different requests that return instances of different models.

The serializer does not support ordering fields or renames them. You must write your own code for this or use an external tool.

+2
source share

JSON is indented, it is just structured data. Browsers or other tools can format JSON so that it looks beautiful, but by default it does not exist. It is also not part of JSON, as formatting is how it looks on the screen. JSON is often handled by other code or services, so they do not care about indentation if the data is structured correctly.

-one
source share

All Articles