Skip to content
Snippets Groups Projects
json_format.py 866 B
Newer Older
  • Learn to ignore specific revisions
  • Jesper Zedlitz's avatar
    Jesper Zedlitz committed
    import json
    from frictionless import Resource
    
    
    def is_valid(resource, file):
        """Check if the HTTP response is a valid JSON document."""
    
        with open(file.name, "rb") as f:
            try:
                json_data = json.load(f)
    
                if (
                    "path" in json_data
                    and "schema" in json_data
                    and (
                        json_data.get("profile") == "tabular-data-resource"
                        or json_data.get("type") == "table"
                    )
                ):
                    # There is a good chance that this is a Frictionless Data Resource.
                    res = Resource(json_data)
                    resource["schema_valid"] = res.validate().valid
                    return resource["schema_valid"]
    
                return True
    
            except Exception as e:
    
    Jesper Zedlitz's avatar
    Jesper Zedlitz committed
                resource["error"] = str(e)
                return False