333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# flat [![Build Status](https://secure.travis-ci.org/hughsk/flat.png?branch=master)](http://travis-ci.org/hughsk/flat)
 
Take a nested Javascript object and flatten it, or unflatten an object with
delimited keys.
 
## Installation
 
``` bash
$ npm install flat
```
 
## Methods
 
### flatten(original, options)
 
Flattens the object - it'll return an object one level deep, regardless of how
nested the original object was:
 
``` javascript
var flatten = require('flat')
 
flatten({
    key1: {
        keyA: 'valueI'
    },
    key2: {
        keyB: 'valueII'
    },
    key3: { a: { b: { c: 2 } } }
})
 
// {
//   'key1.keyA': 'valueI',
//   'key2.keyB': 'valueII',
//   'key3.a.b.c': 2
// }
```
 
### unflatten(original, options)
 
Flattening is reversible too, you can call `flatten.unflatten()` on an object:
 
``` javascript
var unflatten = require('flat').unflatten
 
unflatten({
    'three.levels.deep': 42,
    'three.levels': {
        nested: true
    }
})
 
// {
//     three: {
//         levels: {
//             deep: 42,
//             nested: true
//         }
//     }
// }
```
 
## Options
 
### delimiter
 
Use a custom delimiter for (un)flattening your objects, instead of `.`.
 
### safe
 
When enabled, both `flat` and `unflatten` will preserve arrays and their
contents. This is disabled by default.
 
``` javascript
var flatten = require('flat')
 
flatten({
    this: [
        { contains: 'arrays' },
        { preserving: {
              them: 'for you'
        }}
    ]
}, {
    safe: true
})
 
// {
//     'this': [
//         { contains: 'arrays' },
//         { preserving: {
//             them: 'for you'
//         }}
//     ]
// }
```
 
### object
 
When enabled, arrays will not be created automatically when calling unflatten, like so:
 
``` javascript
unflatten({
    'hello.you.0': 'ipsum',
    'hello.you.1': 'lorem',
    'hello.other.world': 'foo'
}, { object: true })
 
// hello: {
//     you: {
//         0: 'ipsum',
//         1: 'lorem',
//     },
//     other: { world: 'foo' }
// }
```
 
### overwrite
 
When enabled, existing keys in the unflattened object may be overwritten if they cannot hold a newly encountered nested value:
 
```javascript
unflatten({
    'TRAVIS': 'true',
    'TRAVIS_DIR': '/home/travis/build/kvz/environmental'
}, { overwrite: true })
 
// TRAVIS: {
//     DIR: '/home/travis/build/kvz/environmental'
// }
```
 
Without `overwrite` set to `true`, the `TRAVIS` key would already have been set to a string, thus could not accept the nested `DIR` element.
 
This only makes sense on ordered arrays, and since we're overwriting data, should be used with care.
 
 
### maxDepth
 
Maximum number of nested objects to flatten.
 
``` javascript
var flatten = require('flat')
 
flatten({
    key1: {
        keyA: 'valueI'
    },
    key2: {
        keyB: 'valueII'
    },
    key3: { a: { b: { c: 2 } } }
}, { maxDepth: 2 })
 
// {
//   'key1.keyA': 'valueI',
//   'key2.keyB': 'valueII',
//   'key3.a': { b: { c: 2 } }
// }
```
 
## Command Line Usage
 
`flat` is also available as a command line tool. You can run it with 
[`npx`](https://ghub.io/npx):
 
```sh
npx flat foo.json
```
 
Or install the `flat` command globally:
 
```sh
npm i -g flat && flat foo.json
```
 
Accepts a filename as an argument:
 
```sh
flat foo.json
```
 
Also accepts JSON on stdin:
 
```sh
cat foo.json | flat
```