2

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

2
  • Did you create the file by hand? try removing extra spaces after a colon. Commented Jan 16, 2020 at 19:18
  • How do you pass this to your view ? Are you using postman or with python somehow ? You are missing { in front and } at back Commented Jan 16, 2020 at 19:39

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you buddy, there were two mistakes, one was to insert the { } and second was this one, you saved my weeks!
Ha! Thanks for the reminder about the open/close brackets! I forgot those and was getting this error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.