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 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 | URL
): Promise<FileSystemLeft | E.Success<GenericOutput>>

Parameters

  • path : path of the JSON file.

Return value

  • E.Success<GenericOutput> : contenu JSON parse.
  • FileSystemLeft : if reading or parsing fails.

See also

Released under the MIT license.