When working with the Sajn API’s HTML field, you need to properly format your HTML content for inclusion in JSON payloads. This guide provides utility functions and best practices for preparing HTML content.
For Python projects, use this script to process HTML files and generate JSON payloads:
#!/usr/bin/env python3"""Generate JSON payload for updating an HTML document field.Reads HTML from a file and outputs properly formatted JSON."""import jsonimport sysimport osimport redef process_html_content(html_content: str) -> str: """ Process HTML content for Sajn API HTML field. Extracts body content and preserves body styles. """ html_content = html_content.strip() # Check if content has a body tag with attributes body_start_match = re.search(r'<body\s+', html_content, re.IGNORECASE) if not body_start_match: return html_content # Find the end of the opening body tag body_start_pos = body_start_match.start() body_tag_end_pos = html_content.find('>', body_start_pos) if body_tag_end_pos == -1: return html_content # Extract the body tag body_tag = html_content[body_start_pos:body_tag_end_pos + 1] # Find closing body tag body_end_match = re.search( r'</body>', html_content[body_tag_end_pos + 1:], re.IGNORECASE ) if not body_end_match: return html_content # Extract body content body_content_start = body_tag_end_pos + 1 body_content_end = body_tag_end_pos + 1 + body_end_match.start() body_content = html_content[body_content_start:body_content_end].strip() # Extract style attribute from body tag style_match = re.search( r'style\s*=\s*"([^"]*)"', body_tag, re.IGNORECASE | re.DOTALL ) if not style_match: style_match = re.search( r"style\s*=\s*'([^']*)'", body_tag, re.IGNORECASE | re.DOTALL ) # Wrap content in div, preserving body styles if style_match: body_style = style_match.group(1).strip() processed_html = f'<div style="{body_style}">{body_content}</div>' else: processed_html = f'<div>{body_content}</div>' return processed_htmldef create_payload(html_content: str) -> dict: """Create the API payload structure.""" processed_html = process_html_content(html_content) return { "type": "HTML", "fieldMeta": { "type": "HTML", "content": processed_html } }def main(): if len(sys.argv) < 2: print("Usage: python format_html.py <html_file>") sys.exit(1) html_file = sys.argv[1] if not os.path.exists(html_file): print(f"Error: File not found: {html_file}") sys.exit(1) with open(html_file, 'r', encoding='utf-8') as f: html_content = f.read() payload = create_payload(html_content) # Output to file with open('output.json', 'w', encoding='utf-8') as f: json.dump(payload, f, indent=2, ensure_ascii=False) print("Payload written to output.json")if __name__ == "__main__": main()
The API only supports inline CSS styles. External stylesheets and <style> tags are stripped during sanitization. Apply all styles directly to elements using the style attribute.
Test with simple content first
Start with basic HTML to verify your integration works, then gradually add complexity. This makes debugging easier if issues arise.
Validate JSON before sending
Use JSON.stringify() in JavaScript or json.dumps() in Python to ensure your content is properly escaped. Invalid JSON will result in API errors.
Keep content focused
Each HTML field should contain a logical section of content. Use multiple fields with position ordering rather than one massive HTML block.
For a complete reference of allowed HTML tags and CSS properties, see the HTML Fields guide.