# resource/jsonasset (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/resource/jsonasset/

JSON data asset.

## JsonAsset extends TextAsset

JSON data asset.

Example: Load a JSON file and read entries

```js
import { JsonAsset } from "./build/vanilla.js";

const jsonAsset = new JsonAsset();
await jsonAsset.load("examples/timelineshowcase/assets/combo.timeline.json");
if (jsonAsset.isLoaded()) {
	const timelineKeys = Object.keys(jsonAsset.data);
	console.log(timelineKeys.length, jsonAsset.data.name);
}
```

- `data` (field, since 0.0.8): Parsed JSON value. (null before loading or if parsing failed)
  - returns System.Object | null
- `constructor()` (constructor, since 0.0.8): Constructor.
- `async load(assetPath)` (method, since 0.0.8): Loads the asset asynchronously.
  - `assetPath` string: JSON file path. (does nothing if already loaded)
  - returns Promise<void>: A promise resolved once fetched and parsed. (if fetching or parsing fails, it reports with console.error and rejects, staying not loaded)
- `loadFromBytes(assetPath, bytes)` (method, since 0.4.0): Loads from bytes already held. (UTF-8 JSON, if it cannot be parsed, reports it and leaves it unloaded. Same as load)
  - `assetPath` string: Name used for getAssetPath.
  - `bytes` ArrayBuffer | ArrayBufferView: Bytes of the JSON file. (UTF-8)
- `unload()` (method, since 0.0.8): Unloads the asset.
