# flattenjs

> A small simple library to easily flatten / unflatten JSON objects. Uses square brackets in path to preserve arrays.

Latest version **2.1.3** (published 2021-07-01) · ISC license · 0 weekly downloads

## Install

```sh
npm install flattenjs
pnpm add flattenjs
yarn add flattenjs
bun add flattenjs
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.1.3 |
| Published | 2021-07-01 |
| First published | 2017-02-14 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 23.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 10 |
| Author | Rich Somerfield |
| Maintainers | richie5um |

## Links

- npm: https://www.npmjs.com/package/flattenjs
- Repository: https://github.com/richie5um/flattenjs
- Homepage: https://github.com/richie5um/flattenjs#readme
- Issues: https://github.com/richie5um/flattenjs/issues
- npm.io page: https://npm.io/package/flattenjs

## Dependencies (1)

- [lodash](https://npm.io/package/lodash.md) ^4.17.21

## Recent versions

- 2.1.3 (latest) — 2021-07-01
- 2.1.2 — 2020-11-02
- 2.1.1 — 2020-11-02
- 2.1.0 — 2020-11-02
- 2.0.0 — 2019-10-23
- 1.0.4 — 2017-04-12
- 1.0.3 — 2017-02-14
- 1.0.2 — 2017-02-14

## README

# FlattenJS

A small simple library to easily flatten / unflatten JSON objects. Uses square brackets in path to preserve arrays.

> The path format works the same as [RSPath](https://www.npmjs.com/package/rspath).

## Installation

    npm install flattenjs --save

## Updates

* 2.1.0: Added opt-in 'preserveEmpty'. Updated lodash dependency. @adil

## Usage

    import { flatten, inflate } from 'flattenjs'

    // Simple

    var obj = {
        a: true
    };

    var flattened = flatten(obj);
    console.log(flattened);
    // { a: true }

    var unflattened = inflate(flattened);
    console.log(unflattened);
    // { a: true }

    // Arrays

    obj = {
        a: true,
        b: {
            ba: [],
            bb: [0, 1, 2, 3, 4]
        }
    };

    flattened = flatten(obj);
    console.log(flattened);
    // {
    //     'a': true,
    //     'b.bb[0]': 0,
    //     'b.bb[1]': 1,
    //     'b.bb[2]': 2,
    //     'b.bb[3]': 3,
    //     'b.bb[4]': 4
    // }

    unflattened = inflate(flattened);
    console.log(unflattened);
    // { a: true, b: { bb: [ 0, 1, 2, 3, 4 ] } }

    // Arrays and Objects

    obj = {
        a: true,
        b: {
            ba: [{
                baa: [0, 1, 2, 3, 4]
            }, {
                bab: [0, 1, 2, 3, 4]
            }]
        }
    };

    flattened = flatten(obj);
    console.log(flattened);
    // {
    //     'a': true,
    //     'b.ba[0].baa[0]': 0,
    //     'b.ba[0].baa[1]': 1,
    //     'b.ba[0].baa[2]': 2,
    //     'b.ba[0].baa[3]': 3,
    //     'b.ba[0].baa[4]': 4,
    //     'b.ba[1].bab[0]': 0,
    //     'b.ba[1].bab[1]': 1,
    //     'b.ba[1].bab[2]': 2,
    //     'b.ba[1].bab[3]': 3,
    //     'b.ba[1].bab[4]': 4
    // }

    unflattened = inflate(flattened);
    console.log(unflattened);

## Preserve Empty Objects/Arrays

This is now available as an opt-in option in flattenjs@2.1.0. Set the second parameter to 'flatten(obj, true)' to enable the preserveEmpty capability.

    var Flatten = require('./flatten');

    var test = {
        a: { 
            b: 1, 
            c: [], 
            d: {},
            f: [{ g: {} }]
        },
        e: {}
    };

    var flattened = Flatten.flatten(test, true);
    console.log(JSON.stringify(flattened, undefined, 2));

    var inflated = Flatten.inflate(flattened);
    console.log(JSON.stringify(inflated, undefined, 2));

---
_Source: https://npm.io/package/flattenjs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
