def to_json(
        self,
        path: Union[str, os.PathLike[str]],
        fs_kwargs: Optional[dict[str, Any]] = None,
        include_outer_list: bool = True,
    ) -> None:
        """Save chain to a JSON file.

        Parameters:
            path : Path to save the file. This supports local paths as well as
                remote paths, such as s3:// or hf:// with fsspec.
            fs_kwargs : Optional kwargs to pass to the fsspec filesystem, used only for
                write, for fsspec-type URLs, such as s3:// or hf:// when
                provided as the destination path.
            include_outer_list : Sets whether to include an outer list for all rows.
                Setting this to True makes the file valid JSON, while False instead
                writes in the JSON lines format.
        """
        opener = open

        if isinstance(path, str) and "://" in path:
            from datachain.client.fsspec import Client

            fs_kwargs = {
                **self._query.catalog.client_config,
                **(fs_kwargs or {}),
            }

            client = Client.get_implementation(path)

            fsspec_fs = client.create_fs(**fs_kwargs)

            opener = fsspec_fs.open

        headers, _ = self._effective_signals_schema.get_headers_with_length()
        headers = [list(filter(None, header)) for header in headers]

        is_first = True

        with opener(path, "wb") as f:
            if include_outer_list:
                # This makes the file JSON instead of JSON lines.
                f.write(b"[\n")
            for row in self.collect_flatten():
                if not is_first:
                    if include_outer_list:
                        # This makes the file JSON instead of JSON lines.
                        f.write(b",\n")
                    else:
                        f.write(b"\n")
                else:
                    is_first = False
                f.write(orjson.dumps(row_to_nested_dict(headers, row)))
            if include_outer_list:
                # This makes the file JSON instead of JSON lines.
                f.write(b"\n]\n")
Save chain to a JSON file.

        Parameters:
            path : Path to save the file. This supports local paths as well as
                remote paths, such as s3:// or hf:// with fsspec.
            fs_kwargs : Optional kwargs to pass to the fsspec filesystem, used only for
                write, for fsspec-type URLs, such as s3:// or hf:// when
                provided as the destination path.
            include_outer_list : Sets whether to include an outer list for all rows.
                Setting this to True makes the file valid JSON, while False instead
                writes in the JSON lines format.
        
src/datachain/lib/dc/datachain.py