code: def annexjson2result(
      d: dict[str, Any], ds: Dataset, **kwargs: Any
  ) -> dict[str, Any]:
      """Helper to convert an annex JSON result to a datalad result dict
  
      Info from annex is rather heterogeneous, partly because some of it
      our support functions are faking.
  
      This helper should be extended with all needed special cases to
      homogenize the information.
  
      Parameters
      ----------
      d : dict
        Annex info dict.
      ds : Dataset instance
        Used to determine absolute paths for `file` results. This dataset
        is not used to set `refds` in the result, pass this as a separate
        kwarg if needed.
      **kwargs
        Passes as-is to `get_status_dict`. Must not contain `refds`.
      """
      lgr.debug("received JSON result from annex: %s", d)
      messages = []
      res = get_status_dict(**kwargs)
      res["status"] = (
          "ok" if d.get("success", False) is True else "error"
      )
      # we cannot rely on any of these to be available as the feed from
      # git annex (or its wrapper) is not always homogeneous
      if d.get("file"):
          res["path"] = str(ds.pathobj / PurePosixPath(d["file"]))
      if "command" in d:
          res["action"] = d["command"]
      if "key" in d:
          res["annexkey"] = d["key"]
      if "fields" in d:
          # this is annex metadata, filter out timestamps
          res["metadata"] = {
              k: v[0] if isinstance(v, list) and len(v) == 1 else v
              for k, v in d["fields"].items()
              if not k.endswith("lastchanged")
          }
      if d.get("error-messages", None):
          res["error_message"] = "\n".join(
              m.strip() for m in d["error-messages"]
          )
      # avoid meaningless standard messages, and collision with actual error
      # messages
      elif "note" in d:
          note = "; ".join(
              ln
              for ln in d["note"].splitlines()
              if ln != "checksum..."
              and not ln.startswith("checking file")
          )
          if note:
              messages.append(translate_annex_notes.get(note, note))
      if messages:
          res["message"] = "\n".join(m.strip() for m in messages)
      return res
  
imports: from __future__ import annotations
  import logging
  from collections.abc import Iterable, Iterator
  from os.path import isabs, isdir
  from os.path import join
  from os.path import normpath, relpath
  from typing import Any, Optional
  from datalad.distribution.dataset import Dataset
  from datalad.support.exceptions import CapturedException, CommandError, format_oneline_tb
  from datalad.support.path import robust_abspath
  from datalad.utils import PurePosixPath, ensure_list, path_is_subpath
label_desc: Use image node_id 4 for calling a global function with example usage: annexjson2result(d, ds) and returns: res