Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/zerodevx/svelte-json-view

View JSON data prettily
https://github.com/zerodevx/svelte-json-view

json-tree pretty-print svelte vanillajs

Last synced: about 4 hours ago
JSON representation

View JSON data prettily

Awesome Lists containing this project

README

        

# svelte-json-view

> View JSON data prettily.

Display JSON data in a tree-like expandable view. Use as a Svelte component or in Vanilla JS.

Here's a
[demo](https://zerodevx.github.io/json-pretty-print/H4sIAAAAAAAAA7WaXXOkNhaG7_MrtK692FQaAgIE8tU4tmfjxJ7MepykUklqS4Do1g4NHT5sd1L73_foC2g3bXs83krF0wLpICHpOe854tcvEPoL_kfo6N8iPzpGR8SLKWFBESakIJnvJYkfZn4YHS10NVHl_B4qeqa87E27mMShz1InS7PQCUmWOkkUJg4hHityFjCSpoON9iTrxC2Hdl3Tc3M1ZSWrMnnx6O94QXDiJtS22Iis6xt1b9V1m-Ovv96ULOOrusxd0X0d4PsA27psKesFoSnyLT-ty7qRbdOmvqtsvYqtlcHLuqkY-lDWbLi15DBK1aLga1Zyez2r1xtWbeWN9z9c31ycntg7UE2U8noprbXS2JtN3XQiYy60Gsaxqiv10K989A_qh1-i0KMOjnAy9D7PG962so4fxOikgr6d1n3TLdB5Kf5kt6Is-QK943foit-LrF4g6sX-0Dyt-042Pu9QLofNEavEGq3ZEgyxUvzRM9SKqkObphY5hx9rISuIHG1YIxi8ZsR7aIu4vMxysRGtyES1dNHJmnfTK6jol9ACQX85-qMXKOctb3qwCYXWRdd80_CVepeiQ1lfbpjtlO6PNCdtqkfVRQFWGRJNLyvUZQmN-rJk66xGonJ_a34bJqjhS9F2vOFq7WHPJ44XOV5w4yXHPj3GEXLgl-fZ-iXrRNfn8tXD2nSjIMTUt_fqamlvJp4bJ4TG5lbHlnImflUl9djJgIxtuZJkL8diBlPUr8eyHspYZvmkbr1e13k9qSxn40iVfje9KBoBj5x25C_zr9xL-bgZd5b1DUtFt2LobVlv8yNz_7-LAx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/svelte-json-view
```

### Svelte

If you're using this in a Svelte app:

```html

import { JsonView } from '@zerodevx/svelte-json-view'

const json = { foo: 'bar' }

```

### Vanilla JS

For other applications with a bundler:

```js
// Import the compiled code from `/dist`
import { JsonView } from '@zerodevx/svelte-json-view/dist'

const app = new JsonView({
target: document.body // node to render into
})

app.$set({
json: { foo: 'bar' }, // object to prettify
... // any other props
})
```

Or load via CDN:

```html

...




const app = new JsonView({
target: document.querySelector('#target'), // node to render into
props: {
json: { foo: 'bar' }, // object to prettify
... // any other props
}
})

```

## Theming

In general, use CSS variables.

| Name | Default | Description |
| ------------------------------ | -------------- | --------------------------------------------- |
| `--jsonPaddingLeft` | `1rem` | Amount of left padding to apply at each depth |
| `--jsonBorderLeft` | `1px dotted` | Style applied to left border for each depth |
| `--jsonBracketColor` | `currentcolor` | Colour for brackets `[`, `{`, `}` and `]` |
| `--jsonBracketHoverBackground` | `#e5e7eb` | Hover background for brackets |
| `--jsonSeparatorColor` | `currentcolor` | Colour for separators `:` and `,` |
| `--jsonKeyColor` | `currentcolor` | Colour for keys |
| `--jsonValColor` | `#9ca3af` | Default colour for values |
| `--jsonValStringColor` | `#059669` | Colour for `string` values |
| `--jsonValNumberColor` | `#d97706` | Colour for `number` values |
| `--jsonValBooleanColor` | `#2563eb` | Colour for `boolean` values |

To apply your own font family (a monospaced font is recommended) or apply custom CSS var overrides,
try wrapping the component and applying these styles to the wrapper like so:

```html



.wrap {
font-family: monospace;
font-size: 14px;
--jsonBorderLeft: 2px dashed red;
--jsonValColor: blue;
...
}

```

## Props

| Name | Type | Default | Description |
| ------- | -------- | ----------- | -------------------------------- |
| `json` | `object` | `undefined` | Un-stringified object to display |
| `depth` | `number` | `Infinity` | Initial expansion depth |

## Development

Library is packaged via [SvelteKit](https://kit.svelte.dev/docs/packaging). Standard Github
[contribution workflow](https://docs.github.com/en/get-started/quickstart/contributing-to-projects)
applies.

### Tests

End-to-end testing via [Playwright](https://github.com/microsoft/playwright). To run tests
headlessly:

```
$ npm run test
```

## Changelog

Please refer to the [releases](https://github.com/zerodevx/svelte-json-view/releases) page.

## License

ISC