### Install flat package Source: https://github.com/hughsk/flat/blob/master/README.md Install the flat package using npm. ```bash $ npm install flat ``` -------------------------------- ### Install and run flat globally Source: https://github.com/hughsk/flat/blob/master/README.md Install the flat command globally using npm and then execute it with a JSON filename. ```sh npm i -g flat && flat foo.json ``` -------------------------------- ### Run flat as a command-line tool (npx) Source: https://github.com/hughsk/flat/blob/master/README.md Execute the flat command-line tool using npx, passing a JSON filename as an argument. ```sh npx flat foo.json ``` -------------------------------- ### Run flat with JSON from stdin Source: https://github.com/hughsk/flat/blob/master/README.md Pipe JSON content to the flat command-line tool via standard input. ```sh cat foo.json | flat ``` -------------------------------- ### Transform keys during flattening and unflattening Source: https://github.com/hughsk/flat/blob/master/README.md Use the 'transformKey' option to modify keys during both flattening and unflattening operations. Requires 'flat' to be imported. ```javascript import { flatten, unflatten } from 'flat' flatten({ key1: { keyA: 'valueI' }, key2: { keyB: 'valueII' }, key3: { a: { b: { c: 2 } } } }, { transformKey: function(key){ return '__' + key + '__'; } }) ``` ```javascript unflatten({ '__key1__.__keyA__': 'valueI', '__key2__.__keyB__': 'valueII', '__key3__.__a__.__b__.__c__': 2 }, { transformKey: function(key){ return key.substring(2, key.length - 2) } }) ``` -------------------------------- ### Flatten object with maxDepth option Source: https://github.com/hughsk/flat/blob/master/README.md Limit the depth of flattening using the 'maxDepth' option. Requires 'flat' to be imported. ```javascript import { flatten } from 'flat' flatten({ key1: { keyA: 'valueI' }, key2: { keyB: 'valueII' }, key3: { a: { b: { c: 2 } } } }, { maxDepth: 2 }) ``` -------------------------------- ### Flatten object with safe option (preserve arrays) Source: https://github.com/hughsk/flat/blob/master/README.md When the 'safe' option is enabled, arrays and their contents are preserved during flattening. Requires 'flat' to be imported. ```javascript import { flatten } from 'flat' flatten({ this: [ { contains: 'arrays' }, { preserving: { them: 'for you' }} ] }, { safe: true }) ``` -------------------------------- ### Flatten a nested object Source: https://github.com/hughsk/flat/blob/master/README.md Use the flatten method to convert a nested object into a single-level object with delimited keys. Requires 'flat' to be imported. ```javascript import { flatten } from 'flat' flatten({ key1: { keyA: 'valueI' }, key2: { keyB: 'valueII' }, key3: { a: { b: { c: 2 } } } }) ``` -------------------------------- ### Unflatten object with overwrite option Source: https://github.com/hughsk/flat/blob/master/README.md When the 'overwrite' option is enabled, existing keys in the unflattened object can be overwritten if they cannot hold a newly encountered nested value. Use with care. ```javascript unflatten({ 'TRAVIS': 'true', 'TRAVIS.DIR': '/home/travis/build/kvz/environmental' }, { overwrite: true }) ``` -------------------------------- ### Unflatten a delimited object Source: https://github.com/hughsk/flat/blob/master/README.md Use the unflatten method to convert a single-level object with delimited keys back into a nested object. Requires 'flat' to be imported. ```javascript import { unflatten } from 'flat' unflatten({ 'three.levels.deep': 42, 'three.levels': { nested: true } }) ``` -------------------------------- ### Unflatten object with object option (prevent array creation) Source: https://github.com/hughsk/flat/blob/master/README.md When the 'object' option is enabled, arrays are not automatically created during unflattening. This preserves numeric keys as object properties. ```javascript unflatten({ 'hello.you.0': 'ipsum', 'hello.you.1': 'lorem', 'hello.other.world': 'foo' }, { object: true }) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.