code: def to_json(fig, validate=True, pretty=False, remove_uids=True):
      """
      Convert a figure to a JSON string representation
  
      Parameters
      ----------
      fig:
          Figure object or dict representing a figure
  
      validate: bool (default True)
          True if the figure should be validated before being converted to
          JSON, False otherwise.
  
      pretty: bool (default False)
          True if JSON representation should be pretty-printed, False if
          representation should be as compact as possible.
  
      remove_uids: bool (default True)
          True if trace UIDs should be omitted from the JSON representation
  
      Returns
      -------
      str
          Representation of figure as a JSON string
      """
      from _plotly_utils.utils import PlotlyJSONEncoder
  
      # Validate figure
      # ---------------
      fig_dict = validate_coerce_fig_to_dict(fig, validate)
  
      # Remove trace uid
      # ----------------
      if remove_uids:
          for trace in fig_dict.get("data", []):
              trace.pop("uid", None)
  
      # Dump to a JSON string and return
      # --------------------------------
      opts = {"sort_keys": True}
      if pretty:
          opts["indent"] = 2
      else:
          # Remove all whitespace
          opts["separators"] = (",", ":")
  
      return json.dumps(fig_dict, cls=PlotlyJSONEncoder, **opts)
  
imports: from __future__ import absolute_import
  from six import string_types
  import json
  from plotly.io._utils import validate_coerce_fig_to_dict, validate_coerce_output_type
label_desc: Use image node_id 1 for calling a global function with example usage: to_json(fig, validate, pretty, remove_uids) and returns: json