import json
import base64


def to_base64_encoded_json(obj) -> str:
  """Encode a Python object as a base64-endoded JSON string.

  When embedding JSON inline inside HTML, serialize it to a JSON string in
  Python and base64 encode that string to escape it so that it's safe to render
  inside HTML.  Then on the JS side, base64 decode it and parse it as JSON.

  Args:
    obj: any Python object serializable to JSON

  Returns:
    base64-encoded string of JSON
  """
  json_string = json.dumps(obj)
  return base64.b64encode(json_string.encode('utf-8')).decode('utf-8')