Nick
Nick

Reputation: 2369

Validate well-formed JSON using Python

I would like to create a python script that makes a series of web requests to endpoints that will spit out JSON. After receiving the response, I want to validate that it is well-formed JSON, and not some error page. Also, I will need to insert an API key into the request header to get a proper response. what is the best way to go about doing this in python? Thanks!

Upvotes: 0

Views: 1005

Answers (2)

MONTYHS
MONTYHS

Reputation: 924

Validate the json use json.dump(), if your json object incorect it's return the error, handle the error using try catch and answer to your second question , i think you talking about X-AUTH-TOKEN , if your request return json object use this header.

headers = {
"Content-Type": "application/json",
"X-AUTH-TOKEN": your API Token
}

otherwise use

"Content-type":"application/x-www-form-urlencoded"

Upvotes: 1

fasouto
fasouto

Reputation: 4511

To validate the JSON you can use http://python-jsonschema.readthedocs.org/en/latest/

To insert the API key in the header you can use Requests http://docs.python-requests.org/en/latest/user/quickstart/

Upvotes: 1

Related Questions