Handling complex JSON Schemas in Python

Paul Götze
Grammofy
Published in
3 min readJun 28, 2017

--

In a previous post we looked at how to test your Python API app with JSON Schema. In case you have to deal with complex and nested JSON data, schema definitions can get long and confusing. However, there are ways to clean up and reuse your schemas by using references. Let’s see what they are and how you can leverage them in your testing setup.

Internal References

Let’s take an example endpoint GET /recordings/:id which returns a JSON response for a recording of a classical music piece:

We have a work object with a composer and there is a list of recording artists, which include the musicians and technical personnel. All three of them–the composer, the pianist, and the engineer/producer–share the same data structure of an artist.

Using the above JSON response, the corresponding JSON Schema would look like this:

We repeated the schema for artists twice. There is a cleaner way of defining this structure…

--

--

Paul Götze
Grammofy

Authoring code. Drawing stuff. Comics addict and fan of bad jokes. Creator of adoptoposs.org & zarenwitze.de.