Effortlessly transform JSON data into a clear schema - createIT
Get a free advice now!

    Pick the topic
    Developer OutsourcingWeb developingApp developingDigital MarketingeCommerce systemseEntertainment systems

    Thank you for your message. It has been sent.

    Effortlessly transform JSON data into a clear schema

    December 5, 2023
    Last update: August 21, 2024
    6 min read
    20
    0
    0
    Effortlessly transform JSON data into a clear schema

    Challenge: Deciphering complex JSON data

    Solution: Generate schema using Python script

    Navigating through the labyrinth of JSON data can be a daunting task, especially when dealing with complex and nested structures. Enter the challenge: How do you transform a complex JSON file into a format that’s easy to comprehend and work with?

    That’s where our Python script, the “JSON to Schema Converter,” comes into play. Our script takes any JSON file and generates a clean, easy-to-understand schema.

    The need for a JSON schema

    JSON (JavaScript Object Notation) has emerged as a standard format for data interchange, particularly in web applications. Its lightweight nature and human-readable format make it an ideal choice for network communication.

    However, with the convenience of JSON comes a significant challenge—its complexity. As JSON structures grow in size and intricacy, understanding and managing this data becomes increasingly difficult. This complexity is where the need for a JSON schema becomes apparent.

    Understanding JSON complexity

    One of the primary challenges with JSON files is their propensity for deeply nested structures. These complex layers can be difficult to decipher and navigate, often leading to confusion and errors in data interpretation. Furthermore, JSON data does not adhere to a strict schema, resulting in variations in structure.

    Role of a JSON schema

    A JSON schema serves multiple crucial roles in managing and utilizing JSON data effectively. Firstly, it provides a visual representation of the JSON structure, simplifying the understanding of the hierarchy and relationships between different elements. Lastly, a schema is an essential tool for validating JSON data.

    JSON to schema converter

    Our Python script takes a JSON file as input and generates a schema that outlines the structure of the data. This schema is enriched with information about data types and example values for each field, making it easier to understand the structure and content of complex JSON files.

    Technical deep-dive

    By recursively processing each element, whether an object, array, or primitive type, the script ensures a thorough and accurate schema representation.

    When the script encounters a JSON object, it generates a schema with a “properties” field, mapping each key and recursively processing its values. For arrays, it creates an “items” field, capturing the structure of the array’s elements, whether homogeneous or diverse.

    For primitive data types like strings, numbers, and booleans, the script produces a schema with “type” and “example” fields.

    How to use the script

    Using the JSON to schema converter is straightforward.

    Step-by-step setup

    1. Install Python: First, ensure Python is installed on your system. If not, download and install it from the official Python website.
    2. Download the script: Download the json_2_schema.py script from the GitHub repository.
    3. Prepare your JSON file: Ensure you have the JSON file you want to convert ready.
    4. Open your command line interface: Open a terminal (Linux/Mac) or command prompt (Windows).
    5. Navigate to the script’s directory: Use the cd command to navigate to the directory where you’ve saved the script.

    Examples

    Generate schema without type information:

    python json_2_schema.py data/vendor-list.json

    Output:

    {
      "properties": {
        "gvlSpecificationVersion": {
          "example": 42
        },
        ...
      }
    }

    Generate schema with type information:

    python json_2_schema.py data/vendor-list.json --include-type

    Output:

    	{
      "type": "object",
      "properties": {
        "gvlSpecificationVersion": {
          "type": "int",
          "example": 42
        },
        ...
      }
    }

    In both examples, the output is a JSON schema printed to the console. You can redirect the output to a file using > if you want to save it. For example:

    python json_2_schema.py data/vendor-list.json > output.json

    Real-world applications

    The JSON to Schema Converter finds its utility in a variety of real-world scenarios.

    API development and integration

    Developers often use JSON to exchange data between servers and web applications. The script can be used to understand the structure of JSON responses from APIs, aiding in quicker integration and debugging.

    Data cleaning and preprocessing

    Data scientists frequently encounter JSON files in data collection. The script helps in preprocessing steps, allowing for a quick understanding and transformation of JSON data into a workable format.

    Exploratory data analysis

    Analyzing the structure of JSON data is often the first step in exploratory data analysis. The script provides a clear schema that can guide further data analysis tasks.

    JSON2Schema on GitHub

    Empower your data handling capabilities today with the JSON to Schema Converter. Whether you’re tackling complex data structures in your professional work or just passionate about data organization, this tool is designed to simplify your workflow and enhance your understanding of JSON data.

    Visit the GitHub repository to download the script: https://github.com/createit-dev/319-JSON2Schema

    Practical use case

    Analyzing large JSON files, especially those containing extensive data, can be a formidable task. A perfect example is the TCF vendor list JSON, available at https://vendor-list.consensu.org/v3/vendor-list.json . This file, significant in size and data, is an integral part of creating integrations for Consent Management Platforms.

    Utilizing our Python script, we can efficiently decipher the structure and comprehend the data included in such extensive JSON files. The script’s output provides a clear schema, showcasing the JSON object examples and the names of elements, making it far easier to understand and work with the data.

    Here’s a snippet of the script’s output for the TCF vendor list JSON:

    {
       "properties": {
          "gvlSpecificationVersion": {
             "example": 3
          },
          "vendorListVersion": {
             "example": 23
          },
          "tcfPolicyVersion": {
             "example": 4
          },
          "lastUpdated": {
             "example": "2023-10-19T16:07:28Z"
          },
          "purposes": {
             "1": {
                "properties": {
                   "id": {
                      "example": 1
                   },
                   "name": {
                      "example": "Store and/or access information on a device"
                   },
                   "description": {
                      "example": "Cookies, device or similar online identifiers (e.g. login-based identifiers, randomly assigned identifiers, network based identifiers) together with other information (e.g. browser type and information, language, screen size, supported technologies etc.) can be stored or read on your device to recognise it each time it connects to an app or to a website, for one or several of the purposes presented here."
                   },
                   "illustrations": {
                      "items": {
                         "example": "Most purposes explained in this notice rely on the storage or accessing of information from your device when you use an app or visit a website. For example, a vendor or publisher might need to store a cookie on your device during your first visit on a website, to be able to recognise your device during your next visits (by accessing this cookie each time)."
                      }
                   }
                }
             }
          },
          "specialPurposes": {
             "1": {
                "properties": {
                   "id": {
                      "example": 1
                   },
                   "name": {
                      "example": "Ensure security, prevent and detect fraud, and fix errors\n"
                   },
                   "description": {
                      "example": "Your data can be used to monitor for and prevent unusual and possibly fraudulent activity (for example, regarding advertising, ad clicks by bots), and ensure systems and processes work properly and securely. It can also be used to correct any problems you, the publisher or the advertiser may encounter in the delivery of content and ads and in your interaction with them."
                   },
                   "illustrations": {
                      "items": {
                         "example": "An advertising intermediary delivers ads from various advertisers to its network of partnering websites. It notices a large increase in clicks on ads relating to one advertiser, and uses data regarding the source of the clicks to determine that 80% of the clicks come from bots rather than humans."
                      }
                   }
                }
             }
          },
          "features": {
             "1": {
                "properties": {
                   "id": {
                      "example": 1
                   },
                   "name": {
                      "example": "Match and combine data from other data sources"
                   },
                   "description": {
                      "example": "Information about your activity on this service may be matched and combined with other information relating to you and originating from various sources (for instance your activity on a separate online service, your use of a loyalty card in-store, or your answers to a survey), in support of the purposes explained in this notice."
                   },
                   "illustrations": {
                      "items": {}
                   }
                }
             }
          },
          "specialFeatures": {
             "1": {
                "properties": {
                   "id": {
                      "example": 1
                   },
                   "name": {
                      "example": "Use precise geolocation data"
                   },
                   "description": {
                      "example": "With your acceptance, your precise location (within a radius of less than 500 metres) may be used in support of the purposes explained in this notice."
                   },
                   "illustrations": {
                      "items": {}
                   }
                }
             }
          },
          "stacks": {
             "2": {
                "properties": {
                   "id": {
                      "example": 2
                   },
                   "purposes": {
                      "items": {
                         "example": 2
                      }
                   },
                   "specialFeatures": {
                      "items": {}
                   },
                   "name": {
                      "example": "Advertising based on limited data and advertising measurement"
                   },
                   "description": {
                      "example": "Advertising can be presented based on limited data. Advertising performance can be measured."
                   }
                }
             }
          },
          "dataCategories": {
             "1": {
                "properties": {
                   "id": {
                      "example": 1
                   },
                   "name": {
                      "example": "IP addresses"
                   },
                   "description": {
                      "example": "Your IP address is a number assigned by your Internet Service Provider to any Internet connection. It is not always specific to your device and is not always a stable identifier.\nIt is used to route information on the Internet and display online content (including ads) on your connected device."
                   }
                }
             }
          },
          "vendors": {
             "1": {
                "properties": {
                   "id": {
                      "example": 1
                   },
                   "name": {
                      "example": "Exponential Interactive, Inc d/b/a VDX.tv"
                   },
                   "purposes": {
                      "items": {
                         "example": 1
                      }
                   },
                   "legIntPurposes": {
                      "items": {}
                   },
                   "flexiblePurposes": {
                      "items": {
                         "example": 7
                      }
                   },
                   "specialPurposes": {
                      "items": {
                         "example": 1
                      }
                   },
                   "features": {
                      "items": {
                         "example": 1
                      }
                   },
                   "specialFeatures": {
                      "items": {}
                   },
                   "cookieMaxAgeSeconds": {
                      "example": 7776000
                   },
                   "usesCookies": {
                      "example": true
                   },
                   "cookieRefresh": {
                      "example": true
                   },
                   "usesNonCookieAccess": {
                      "example": false
                   },
                   "dataRetention": {
                      "properties": {
                         "stdRetention": {
                            "example": 397
                         },
                         "purposes": {
                            "properties": {}
                         },
                         "specialPurposes": {
                            "properties": {}
                         }
                      }
                   },
                   "urls": {
                      "items": {
                         "properties": {
                            "langId": {
                               "example": "en"
                            },
                            "privacy": {
                               "example": "https://vdx.tv/privacy/"
                            },
                            "legIntClaim": {
                               "example": "https://cdnx.exponential.com/wp-content/uploads/2018/04/Balancing-Assessment-for-Legitimate-Interest-Publishers-v2.pdf"
                            }
                         }
                      }
                   },
                   "dataDeclaration": {
                      "items": {
                         "example": 1
                      }
                   },
                   "deviceStorageDisclosureUrl": {
                      "example": "https://vdxtv.expo.workers.dev"
                   }
                }
             }
          }
       }
    }

    That’s it for today’s tutorial. Are you looking for skilled developers to join your team? Would you like us to bring your dream project to life? Contact us.

    Support – Tips and Tricks
    All tips in one place, and the database keeps growing. Stay up to date and optimize your work!

    Contact us