code: def _prepare_write_metadata(metadata):
      """Convert metadata to JSON for saving."""
      if metadata is not None:
          if not isinstance(metadata, list):
              metadata = metadata.to_json(orient="records")
          else:  # Pandas DataFrame
              metadata = json.dumps(metadata)
          assert isinstance(metadata, str)
      return metadata
  
imports: import json
  import logging
  from collections import OrderedDict
  from copy import deepcopy
  import numpy
  from ._logging import verbose, warn
  from .check import _check_pandas_installed, _check_preload, _validate_type
  from .numerics import _time_mask, object_hash, object_size
label_desc: Use image node_id 2 for calling a global function with example usage: _prepare_write_metadata(metadata) and returns: metadata