Skip to content

readJsonFile ​

Reads and parses a JSON file.

Example ​

ts
import { 
SF
} from "@duplojs/server-utils";
import {
E
, type
ExpectType
,
unwrap
} from "@duplojs/utils";
const
config
= await
SF
.
readJsonFile
("/tmp/config.json");
// config: E.Success<unknown> | SF.FileSystemLeft<"read-json-file"> const
result
= await
SF
.
readJsonFile
<{
content
: string }>("/tmp/data.json");
if (
E
.
isRight
(
result
)) {
const
data
=
unwrap
(
result
);
type
check
=
ExpectType
<
typeof
data
,
{
content
: string },
"strict" >; }

Syntax ​

typescript
function readJsonFile<GenericOutput>(
  path: string
): Promise<FileSystemLeft<"read-json-file"> | E.Success<GenericOutput>>

Parameters ​

  • path : path of the JSON file.

Return value ​

  • E.Success<GenericOutput> : contenu JSON parse.
  • FileSystemLeft<"read-json-file"> : if reading or parsing fails.

See also ​

Released under the MIT license.