PyJError when using the toJson method in standard access mode compute

Change the method from toJson to safeToJson.

Written by Gihyeon Lee

Last published at: March 8th, 2025

Problem

You are using the toJson() method with dbutils in a Python notebook on a standard (formerly shared) access mode compute cluster.

dbutils.notebook.entry_point.getDbutils().notebook().getContext().toJson()

Upon executing, you receive a PyJError.

PyJError: An error occurred while calling 0450.toJson.
Trace: py4j.security.Py4JSecurityException: Method public java.lang.String com.databricks.backend.common.rpc.CommandContext.toJson() is not allowlisted on class class com.databricks.backend.common.rpc.CommandContext

 

Cause

The toJson() method is not allowlisted on standard access mode clusters.

 

Solution

Change the method from toJson() to safeToJson() with Databricks Runtime 13.3 LTS or above. This method is allowed in the standard access mode. It provides a subset of all command context information that can be securely shared on the cluster.

dbutils.notebook.entry_point.getDbutils().notebook().getContext().safeToJson()