Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8852845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:30:10+00:00 2026-06-14T13:30:10+00:00

I am writing an API and expecting data in JSON. My function works well

  • 0

I am writing an API and expecting data in JSON. My function works well and stores data in SQLite as follows:

if request.method == 'POST':
    if request.headers['Content-Type'] == 'application/json':
        db = get_db()
        data = json.loads(request.data)
        row = (data['lat'], data['long'], data['address'], data['name'])
        db.execute('INSERT INTO places (lat, long, address, name) values (?, ?, ?, ?)', row)
        db.commit()
        resp = Response(status=200, mimetype='application/json')
        return resp

If someone sends a POST with incorrect JSON fields (missing lat, long, address or name), then an error is thrown by Flask.

What’s the best way to handle this?

I tried doing:

if not 'lat' in data or if not 'long' in data or ....

But data is just a string and not a dictionary. So I have two questions:

  1. How are the filed being references as if its a dictionary above (data['lat']…)?
  2. What is an appropriate way to handle this error?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:30:11+00:00Added an answer on June 14, 2026 at 1:30 pm

    The moment you load data from JSON with data = json.loads(request.data) you have a python structure.

    If at that time it is not a dictionary, then whatever the request sent you did not hold the correct JSON structure (could be a list, for example).

    I’d use a try / execept in this case:

    try:
        data = json.loads(request.data)
        row = (data['lat'], data['long'], data['address'], data['name'])
    except (ValueError, KeyError, TypeError):
        # Not valid information, bail out and return an error
        return SomeErrorResponse
    

    An exception will be raised if request.data is not valid JSON, or if data is not a dictionary with the correct keys. The three exceptions listed are what would be raised by the various error modes that are possible:

    >>> import json
    >>> json.loads('nonsense')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/__init__.py", line 307, in loads
        return _default_decoder.decode(s)
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py", line 319, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/json/decoder.py", line 338, in raw_decode
        raise ValueError("No JSON object could be decoded")
    ValueError: No JSON object could be decoded
    >>> 'ouea'['abc']
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: string indices must be integers, not str
    >>> [0]['oue']
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: list indices must be integers, not str
    >>> {}['oue']
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    KeyError: 'oue'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Web API service and trying to return a (400) Bad Request
I am writing an API in node.js that pulls data from a db, manipulates
I'm writing API implementation and my main API class has __call() magic method: public
I'm writing an API which is used to receive some data from another application.
I'm writing API for a mongo database. I need to pass JSON object as
Let's consider the following example. Writing API which has public method which returns Collection
I am writing an API which includes IPC functions which send data to another
I am writing an app to query the Google shopping api. Using the request
I'm writing an API method that returns a list of objects from a web
I'm writing an API in Flask, and my HTTP DELETE method deletes a record

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.