Brij
Brij

Reputation: 43

How to parse a JSON in django rest framework

I am a noobie in DRF, so please forgive me if this is a silly one, but this is my JSON file :

"articles": [
        {
            "source": {
                "id": "techcrunch",
                "name": "TechCrunch"
            },
            "author": "Jonathan Shieber",
            "title": "All tulips must wilt",
            "description": "It’s a bad day in the world of cryptocurrency. After recovering some during the summer, the value of bitcoin and other cryptocurrencies are sharply down over the last several weeks. Looking back a month, bitcoin was worth around $8,500 a coin. Today it’s wort…",
            "url": "http://techcrunch.com/2019/12/18/all-tulips-must-wilt/",
            "urlToImage": "https://techcrunch.com/wp-content/uploads/2019/12/Screen-Shot-2019-12-18-at-9.39.21-AM.png?w=669",
            "publishedAt": "2019-12-18T15:17:17Z",
            "content": "It’s a bad day in the world of cryptocurrency. After recovering some during the summer, the value of bitcoin and other cryptocurrencies are sharply down over the last several weeks. Looking back a month, bitcoin was worth around $8,500 a coin. Today it’s wort… [+673 chars]"
        },
        {
            "source": {
                "id": "mashable",
                "name": "Mashable"
            },
            "author": "Stan Schroeder",
            "title": "Bitcoin whale moves $1.1 billion in bitcoins for an $80 fee",
            "description": "Bitcoin hasn't (yet) fulfilled its original promise of being widely-used electronic cash, but it still offers some features that would be hard to achieve within the traditional banking system. Namely, moving $1.1 billion from one address to another, in a sing…",
            "url": "https://mashable.com/article/bitcoin-1-1-billion-transaction/",
            "urlToImage": "https://mondrian.mashable.com/2020%252F01%252F15%252F38%252Fd26e834787934c56a33fdeb39faa0be8.84e34.jpg%252F1200x630.jpg?signature=IHj6xz7nTFxvmjn6XOvUiHKJCIM=",
            "publishedAt": "2020-01-15T09:10:59Z",
            "content": "Bitcoin hasn't (yet) fulfilled its original promise of being widely-used electronic cash, but it still offers some features that would be hard to achieve within the traditional banking system. Namely, moving $1.1 billion from one address to another, in a sing… [+1589 chars]"
        },

    ]

And this is my views.py

@api_view(['GET','POST'])
def index(request):
    if(request.method == 'POST'):
        print(request.data)
        return Response({"message": "Got some data!"})
    else:
        message = 'This is a testing message'
        return Response(data = message, status=status.HTTP_200_OK)

The error which I am getting here is :

{
    "detail": "JSON parse error - Extra data: line 1 column 11 (char 10)"
}

Please help me out

Upvotes: 2

Views: 2386

Answers (1)

Maede
Maede

Reputation: 182

instead of using request.data try

req_body = json.loads(request.body.decode('utf-8'))

and then you can use req_body like dict in python

Upvotes: 1

Related Questions