02 August 2016   0 comments   Python


Last week I made a little library called json-schema-reducer. It's a simple function that takes a JSON Schema and dict (or a JSON string or a .json file path), and makes a new dict that only contains the keys listed in the JSON Schema.

This is handy if you have a JSON Schema which dictates what you can/want to share/publish/save, but you have a data structure that contains keys and values you don't want to share/publish/save.

I built this because there are a couple of projects that can turn data structures into models from a JSON Schema but none that have the ability to reduce stuff from a data structure. Here's an example:

Sample JSON Schema (schema.json)

    "type": "object", 
    "$schema": "http://json-schema.org/draft-04/schema#", 
    "title": "Sample JSON Schema", 
    "required": [
    "properties": {
        "name": {
            "type": "string"
        "sex": {
            "type": "string"
        "title": {
            "type": "string"

Sample data structure (sample.json)

    "name": "Peter",
    "sex": "male",
    "email": "peterbe@example.com",


>>> from json_schema_reducer import make_reduced_dict
>>> make_reduced_dict('schema.json', 'sample.json')
{'name': 'Peter', 'sex': 'male'}  # Note! No "email" key

The project works in Python 2 and Python 3. See tests.

Also, the function tries to be convenient in that it can accept either a dict, a JSON string or path to a .json file.


Your email will never ever be published

Related posts

Premailer 3.0.0 - classes kept by default 07 June 2016
How to identify/classify what language a piece of text is 09 August 2016
Related by Keyword:
Msgpack vs JSON (with gzip) 19 December 2017
Fastest Redis configuration for Django 11 May 2017
Cope with JSONDecodeError in requests.get().json() in Python 2 and 3 16 November 2016
Gzip rules the world of optimization, often 09 August 2014
Migration of Postgres 9.2 to 9.3 with Homebrew and json_enhancements 30 April 2014
Related by Text:
Be very careful with your add_header in Nginx! You might make your site insecure 11 February 2018
jQuery and Highslide JS 08 January 2008
I'm back! Peterbe.com has been renewed 05 June 2005
Anti-McCain propaganda videos 12 August 2008
I'm Prolog 01 May 2007