Nested json in django rest framework

142 views
Skip to first unread message

Koushik Romel

unread,
Jun 5, 2022, 1:06:31 AM6/5/22
to Django users
I need to achieve the following structure using Django restframework anyone help with the method or share me the video or blog links it'll be very much helpful for me
I want those #One and #Two to be in curly braces {} and not as square bracket [] , and Thanks in advance
[ { 
"id": 123,
"city": "Sidney", "
personInCity": [ #One
     {
              "id": 1, 
              "city": "Sidney", 
               "personName": "Giles",
                "dob": "2011-02-02" 
      }, 
     {
            "id": 5,
             "city": "Paulba", 
              "personName": "Sidney",
             "dob": "2016-07-16" 
      }
 ] #Two
 } ] 

Kasper Laudrup

unread,
Jun 5, 2022, 1:45:47 PM6/5/22
to django...@googlegroups.com
On 05/06/2022 07.06, Koushik Romel wrote:
> I want those #One and #Two to be in curly braces {} and not as square
> bracket [] , and Thanks in advance

A good start would be to understand what the difference between square
brackets and curly braces is. The JSON format is very simple:

https://en.wikipedia.org/wiki/JSON#Syntax

So you actually want to convert your array to an object with anonymous
properties/members. That doesn't really make any sense and wouldn't be
valid JSON.

Of course you could write a dirty hack and do it anyway but I seriously
doubt that's really what you want.

Hope that helps.

Kind regards,

Kasper Laudrup
OpenPGP_0xE5D9CAC64AAA55EB.asc
OpenPGP_signature
Message has been deleted

Koushik Romel

unread,
Jun 6, 2022, 12:42:09 AM6/6/22
to Django users
Thanks for your time Kasper, I've learnt something new from your message and I solved the problem yesterday itself it was my mistake I added the wrong model to serializer

Amitesh Sahay

unread,
Jun 6, 2022, 1:18:26 AM6/6/22
to Django users
PROBABLY, Pandas would help you

Regards,
Amitesh Sahay
91-750 797 8619


On Monday, 6 June, 2022, 10:12:07 am IST, Koushik Romel <koushi...@gmail.com> wrote:


Thanks for your time I've learnt something new from your message and I solved the problem yesterday itself it was my mistake I added the wrong model to serializer

On Sunday, 5 June 2022 at 23:15:47 UTC+5:30 Kasper Laudrup wrote:

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/1e14a36d-0960-4083-a208-9396340c3912n%40googlegroups.com
.
Reply all
Reply to author
Forward
0 new messages