Skip to main content

Handling JSON


Where appropriate, the Cortex Data Lake REST API accepts and returns JSON payloads/responses, a format that pairs well with the Python programming language.

The Basics#

So you want to convert your JSON response into a python object? With requests under the hood, it’s downright easy:

q = qs.create_query(query_params={"query": SQL})raw = q.textdictionary = q.json()

Let's take a look at the difference between raw and dictionary shall we?

raw (formatted for display):

{  "jobId": "15075d1e-059f-475b-89c2-6aae23483059",  "uri": "/query/v2/jobs/15075d1e-059f-475b-89c2-6aae23483059"}


{'jobId': '15075d1e-059f-475b-89c2-6aae23483059', 'uri': '/query/v2/jobs/15075d1e-059f-475b-89c2-6aae23483059'}

Easy right?

Enforce JSON#

What if you want to enforce proper JSON encoding on all responses?

qs = QueryService(    credentials=Credentials(),    enforce_json=True  # Attempts json.dumps() on responses)

Now, if a response returns something not JSON-serializable, the CDL Python SDK will raise a CortexError that can be handled, appropriately, in your client code.


Looking for an easy, cheap way to boost your JSON encoder/decoder? Install ujson and the CDL Python SDK will automatically use it in place of the standard library json module.


Visit the UltraJSON GitHub Repo for more details on installation and usage.


pip install ujson

So what's happening under the hood? Basically, something similar to this:

import requestsimport ujson

requests.models.json = ujson

If you have debug logging enabled then you'll see the following message logged to your console:

DEBUG:pan_cortex_data_lake.httpclient:Monkey patched requests with ujson