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

https://github.com/chientrm/json2dir

Convert JSON object to directory tree. Auto mkdir recursively.
https://github.com/chientrm/json2dir

fs js json serialize

Last synced: 5 months ago
JSON representation

Convert JSON object to directory tree. Auto mkdir recursively.

Awesome Lists containing this project

README

          

# json2dir

[![CI](https://github.com/chientrm/json2dir/actions/workflows/ci.yml/badge.svg)](https://github.com/chientrm/json2dir/actions/workflows/ci.yml)
[![](https://img.shields.io/bundlephobia/min/json2dir)](https://www.npmjs.com/package/json2dir)
[![](https://img.shields.io/discord/457912077277855764)](https://discord.com/invite/yy75DKs)
[![](https://img.shields.io/npm/v/json2dir)](https://www.npmjs.com/package/json2dir)
[![](https://img.shields.io/npm/dt/json2dir)](https://www.npmjs.com/package/json2dir)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/e3c021b173854e749d48bb8db2a789ed)](https://www.codacy.com/gh/chientrm/json2dir/dashboard?utm_source=github.com&utm_medium=referral&utm_content=chientrm/json2dir&utm_campaign=Badge_Grade)
[![Codacy Badge](https://app.codacy.com/project/badge/Coverage/e3c021b173854e749d48bb8db2a789ed)](https://www.codacy.com/gh/chientrm/json2dir/dashboard?utm_source=github.com&utm_medium=referral&utm_content=chientrm/json2dir&utm_campaign=Badge_Coverage)

- Convert JSON object to directory tree
- Auto `mkdir` recursively
- Zero dependencies
- Lightweight ~1 KB

## Install

```bash
npm install json2dir
```

## Types

```ts
json2dir(
dir: string,
serializers: Record string>,
obj: any
) : void
```

## Usage

```ts
import json2dir from 'json2dir'
import ini from 'ini'

const bob = { name: 'Bob', age: 23 }

json2dir(
'/root',
{
'.ini': ini.encode,
'.json': JSON.stringify
},
{
'a.json': bob,
'b.ini': { INFO: bob },
c: { d: { 'e.json': bob } },
'.sub/foo.bar/f.json': bob
}
)
```

### Results

```bash
/root
│ a.json
│ b.json

└───c/d
│ │
│ │ e.json

└───.sub/foo.bar

│ f.json
```

### Exceptions

If serializer is not provided, an exception will occur.
For example: `Error: No serializer for extension '.json'`

## Sponsor

Buy Me A Coffee