code: def data_to_json(data):
      """
      Turn data (class parameters) into a JSON string for storing
  
      :param data: (Dict) Dictionary of class parameters to be
          stored. Items that are not JSON serializable will be
          pickled with Cloudpickle and stored as bytearray in
          the JSON file
      :return: (str) JSON string of the data serialized.
      """
      # First, check what elements can not be JSONfied,
      # and turn them into byte-strings
      serializable_data = {}
      for data_key, data_item in data.items():
          # See if object is JSON serializable
          if is_json_serializable(data_item):
              # All good, store as it is
              serializable_data[data_key] = data_item
          else:
              # Not serializable, cloudpickle it into
              # bytes and convert to base64 string for storing.
              # Also store type of the class for consumption
              # from other languages/humans, so we have an
              # idea what was being stored.
              base64_encoded = base64.b64encode(
                  cloudpickle.dumps(data_item)
              ).decode()
  
              # Use ":" to make sure we do
              # not override these keys
              # when we include variables of the object later
              cloudpickle_serialization = {
                  ":type:": str(type(data_item)),
                  ":serialized:": base64_encoded,
              }
  
              # Add first-level JSON-serializable items of the
              # object for further details (but not deeper than this to
              # avoid deep nesting).
              # First we check that object has attributes (not all do,
              # e.g. numpy scalars)
              if hasattr(data_item, "__dict__") or isinstance(
                  data_item, dict
              ):
                  # Take elements from __dict__ for custom classes
                  item_generator = (
                      data_item.items
                      if isinstance(data_item, dict)
                      else data_item.__dict__.items
                  )
                  for variable_name, variable_item in item_generator():
                      # Check if serializable. If not, just include the
                      # string-representation of the object.
                      if is_json_serializable(variable_item):
                          cloudpickle_serialization[
                              variable_name
                          ] = variable_item
                      else:
                          cloudpickle_serialization[
                              variable_name
                          ] = str(variable_item)
  
              serializable_data[data_key] = cloudpickle_serialization
      json_string = json.dumps(serializable_data, indent=4)
      return json_string
  
file_path: stable-baselines/stable_baselines/common/save_util.py
imports: import base64
  from collections import OrderedDict
  import io
  import json
  import pickle
  import cloudpickle
  import numpy
label_desc: Use image node_id 2 for calling a global function with example usage: data_to_json(data) and returns: json_string