aboutsummaryrefslogtreecommitdiff
path: root/docs/guides/read-file/json.md
blob: 3b7488472bf348a4f94b45c7e6e6eee7ac41a66e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
---
name: Read a JSON file
---

The `Bun.file()` function accepts a path and returns a `BunFile` instance. The `BunFile` class extends `Blob` and allows you to lazily read the file in a variety of formats. Use `.json()` to read and parse the contents of a `.json` file as a plain object.

```ts
const path = "/path/to/package.json";
const file = Bun.file(path);

const contents = await file.json();
// { name: "my-package" }
```

---

The MIME type of the `BunFile` will be set accordingly.

```ts
const path = "/path/to/package.json";
const file = Bun.file(path);

file.type; // => "application/json;charset=utf-8";
```

---

If the path to the `.json` file is static, it can be directly imported as a module.

```ts
import pkg from "./package.json";

pkg.name; // => "my-package"
```
f39edbb7bc35f0ac51b&follow=1'>Pass `tar -C`Gravatar Jarred Sumner 4-6/+53 2022-11-06[TypeScript] Fix `export = value`Gravatar Jarred Sumner 2-0/+5 2022-11-06Fix artifact name, use tarballGravatar Jarred Sumner 3-12/+11 2022-11-06Try tarballGravatar Jarred Sumner 1-4/+4 2022-11-06Try againGravatar Jarred Sumner 4-0/+4 2022-11-06Node12 is deprecatedGravatar Jarred Sumner 4-28/+32 2022-11-06feat(child_process): add node:child_process polyfill (#1424)Gravatar Derrick Farris 4-3/+2756 2022-11-06fix(subprocess): fix typo in spawnSync (#1464)Gravatar Derrick Farris 1-2/+2 2022-11-05Fix fetch api to accept stringifier object (#1460)Gravatar zhiyuan 2-11/+25 2022-11-03Fix crash in setTimeout/setIntervalGravatar Jarred Sumner 1-0/+5 2022-11-03Fix incorrect exit codeGravatar Jarred Sumner 1-9/+15 2022-11-03Fix crash when reading promise value wihtout checking if nullGravatar Jarred Sumner 1-0/+1 2022-11-03Fix `which` not handling absolute paths to a binGravatar Jarred Sumner 1-3/+20 2022-11-03Fix `cmd not found in $PATH` printing raw structGravatar Jarred Sumner 1-1/+1 2022-11-03Delete unused fileGravatar Jarred Sumner 1-97/+0 2022-11-03Add minified prod build of react-dom/server.bun (#1458)Gravatar Colin McDonnell 2-6819/+138 2022-11-03chore: remove unecessary files (#1457)Gravatar Hyro 2-118/+0 2022-11-03Introduce `import.meta.primordials` for builtin JSGravatar Jarred Sumner 5-3/+95