Debugging JSON

There are two main classes of JSON problems that you may encounter when using the API:

Requests containing invalid JSON

These kinds of errors - where the API returns a result -> message_code of invalid_json - usually occur when developers try to construct the JSON themselves by hand - as opposed to using whatever JSON library their programming language provides to do the work for you.

Hand crafting JSON is when you create the JSON structure yourself, by concatenating the various {, }, commas, and so on, with the actual values you want to send.

It is theoretically possible to create the JSON by hand, but it is error prone, and it introduces a number of other issues, with injection/escaping values and so on.

It is much, much better to use whatever inbuilt JSON library or module that your programming language provides to do all of this work for you. You would normally create a "dictionary", or "array" (depending on what your language calls it), and then somehow "convert" that structure into proper JSON. Refer to your programming language's instructions for more information.

Requests with with an incorrect JSON structure

This kind of error occurs when you are technically sending valid JSON, but the "structure" of it is not what we expect, and so can't work with it.

For example, this response shows that you made a request to the Detect end point, but didn't include the headers key in your request. Maybe you spelt it wrong, maybe it's accidentally nested under a different key, and so on.

{ "result": { "code": "error", "message_code": "headers_not_sent", "message": "Your visitor's headers were missing from your request" } }

So while it seems you sent valid JSON, you haven't sent it in the right structure.

Please double check your code, refer to the Sample Code, and try again.