Kiran Prajapati
Kiran Prajapati

Reputation: 191

How to remove [ ] closed bracket in djagno rest framework output

Is there any way to remove [] in the output when we run server using django.

I need output only in json format, I dont need open and closed bracket in start and end position of output

Here is my output after run the server : http:127.0.0.1:8000/WBCIS/wbcis/

[
    {
        "id": 1,
        "fruit": "Pomegranate",
        "district": "Pune",
        "taluka": "Haveli",
        "revenue_circle": "Uralikanchan",
        "sum_insured": 110000.0,
        "area": 12200.0,
        "farmer": 1836
    },
    {
        "id": 2,
        "fruit": "Guava",
        "district": "Pune",
        "taluka": "Haveli",
        "revenue_circle": "Uralikanchan",
        "sum_insured": 50000.0,
        "area": 1150.0,
        "farmer": 10
    }
]

this is my current output, but i want data in this format, like :

{
    {
        "id": 1,
        "fruit": "Pomegranate",
        "district": "Pune",
        "taluka": "Haveli",
        "revenue_circle": "Uralikanchan",
        "sum_insured": 110000.0,
        "area": 12200.0,


          "farmer": 1836
        },
        {
            "id": 2,
            "fruit": "Guava",
            "district": "Pune",
            "taluka": "Haveli",
            "revenue_circle": "Uralikanchan",
            "sum_insured": 50000.0,
            "area": 1150.0,
            "farmer": 10
        }
    }

Any idea how to remove [ ] brackets ?

Upvotes: 0

Views: 1164

Answers (1)

Ihor Pomaranskyy
Ihor Pomaranskyy

Reputation: 5611

The format you have requested is NOT valid JSON.

Currently you receive data in square brackets [] because you have a list of elements, ordered set of elements accessible by index. And this is valid JSON.

Curly brackets {} are used in dictionaries — sets of elements, accessible by keys.

So, if you want your elements to be placed in curly brackets, they should be put as values of some dictionary. But dictionary elements, of course, should have keys. I.e., data should like this:

{
    "0": {
        "id": 1,
        "fruit": "Pomegranate",
        "district": "Pune",
        "taluka": "Haveli",
        "revenue_circle": "Uralikanchan",
        "sum_insured": 110000.0,
        "area": 12200.0,
        "farmer": 1836
    },
    "1": {
        "id": 2,
        "fruit": "Guava",
        "district": "Pune",
        "taluka": "Haveli",
        "revenue_circle": "Uralikanchan",
        "sum_insured": 50000.0,
        "area": 1150.0,
        "farmer": 10
    }
}

But. The data you currently have are in valid JSON format and are generally OK.

Upvotes: 1

Related Questions