code: def explanation_to_json(
      explanation: Explanation,
      labels: List[int],
      predictions: np.ndarray,
      mode: str,
  ) -> Dict[int, Dict]:
      """
      Parses LIME explanation to produce JSON-parseable output format.
  
      ### Schema for classification:
      {
          class_idx: {OUTPUT.EXPLANATION: [
                      {
                      OUTPUT.FEATURE: str,
                      OUTPUT.SCORE: float
                      },
                        ...
                      ],
          OUTPUT.PREDICTION: float},
          ...
      }
  
      ### Schema for regression:
      {
          OUTPUT.EXPLANATION: [
              {
                  OUTPUT.FEATURE: str,
                  OUTPUT.SCORE: float
              }
              ...
          ],
          'predictions': float
      }
  
      Args:
          explanation (lime.explanation.Explanation): The explanation output from LIME
          labels (list): List of labels for which to get explanations
          predictions (np.ndarray): Model output for a particular instance, which should be a list
          of confidences that sum to one (if classification)
          mode (str): Regression or classification
  
      Returns:
          (dict) Explanations in JSON format
      """
      dict_explanation = {}
  
      if mode == MODE.CLASSIFICATION:
          for label in labels:
              list_explanations = explanation.as_list(label)
              tmp = []
              for exp in list_explanations:
                  tmp.append(
                      {
                          OUTPUT.FEATURE: str(exp[0]),
                          OUTPUT.SCORE: float(exp[1]),
                      }
                  )
              dict_explanation[label] = {
                  OUTPUT.PREDICTION: predictions[label],
                  OUTPUT.EXPLANATION: sorted(
                      tmp, key=lambda x: x[OUTPUT.SCORE], reverse=True
                  ),
              }
      else:
          list_explanations = explanation.as_list()
          tmp = []
          for exp in list_explanations:
              tmp.append(
                  {
                      OUTPUT.FEATURE: str(exp[0]),
                      OUTPUT.SCORE: float(exp[1]),
                  }
              )
          dict_explanation = {
              OUTPUT.PREDICTION: predictions,
              OUTPUT.EXPLANATION: sorted(
                  tmp, key=lambda x: x[OUTPUT.SCORE], reverse=True
              ),
          }
  
      return dict_explanation
  
imports: from typing import List, Dict, Optional, Any
  import numpy
  from lime.explanation import Explanation
  from xai.explainer.constants import MODE, OUTPUT
label_desc: Use image node_id 1 for calling a global function with example usage: explanation_to_json(explanation, labels, predictions, mode) and returns: dict_explanation