{"id":13468262,"url":"https://github.com/hughsk/flat","last_synced_at":"2025-05-13T17:05:20.131Z","repository":{"id":4936633,"uuid":"6093469","full_name":"hughsk/flat","owner":"hughsk","description":":steam_locomotive: Flatten/unflatten nested Javascript objects","archived":false,"fork":false,"pushed_at":"2024-09-16T10:26:32.000Z","size":361,"stargazers_count":1801,"open_issues_count":54,"forks_count":199,"subscribers_count":19,"default_branch":"master","last_synced_at":"2025-05-10T01:38:33.417Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"IBM-Bluemix/phonebot","license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hughsk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2012-10-05T16:51:00.000Z","updated_at":"2025-05-05T06:10:11.000Z","dependencies_parsed_at":"2024-01-15T20:48:32.681Z","dependency_job_id":"bb06eec8-6f0a-41b3-b80e-84e9ee522697","html_url":"https://github.com/hughsk/flat","commit_stats":{"total_commits":111,"total_committers":28,"mean_commits":"3.9642857142857144","dds":0.6666666666666667,"last_synced_commit":"9d8da940f04416b1f560899de414465ea86e1e52"},"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hughsk%2Fflat","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hughsk%2Fflat/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hughsk%2Fflat/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hughsk%2Fflat/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hughsk","download_url":"https://codeload.github.com/hughsk/flat/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253990460,"owners_count":21995774,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T15:01:07.816Z","updated_at":"2025-05-13T17:05:20.104Z","avatar_url":"https://github.com/hughsk.png","language":"JavaScript","readme":"# flat [![Build Status](https://github.com/hughsk/flat/actions/workflows/main.yml/badge.svg)](https://github.com/hughsk/flat/actions/workflows/main.yml)\n\nTake a nested Javascript object and flatten it, or unflatten an object with\ndelimited keys.\n\n## Installation\n\n``` bash\n$ npm install flat\n```\n\n## Methods\n\n### flatten(original, options)\n\nFlattens the object - it'll return an object one level deep, regardless of how\nnested the original object was:\n\n``` javascript\nimport { flatten } from 'flat'\n\nflatten({\n    key1: {\n        keyA: 'valueI'\n    },\n    key2: {\n        keyB: 'valueII'\n    },\n    key3: { a: { b: { c: 2 } } }\n})\n\n// {\n//   'key1.keyA': 'valueI',\n//   'key2.keyB': 'valueII',\n//   'key3.a.b.c': 2\n// }\n```\n\n### unflatten(original, options)\n\nFlattening is reversible too, you can call `unflatten` on an object:\n\n``` javascript\nimport { unflatten } from 'flat'\n\nunflatten({\n    'three.levels.deep': 42,\n    'three.levels': {\n        nested: true\n    }\n})\n\n// {\n//     three: {\n//         levels: {\n//             deep: 42,\n//             nested: true\n//         }\n//     }\n// }\n```\n\n## Options\n\n### delimiter\n\nUse a custom delimiter for (un)flattening your objects, instead of `.`.\n\n### safe\n\nWhen enabled, both `flat` and `unflatten` will preserve arrays and their\ncontents. This is disabled by default.\n\n``` javascript\nimport { flatten } from 'flat'\n\nflatten({\n    this: [\n        { contains: 'arrays' },\n        { preserving: {\n              them: 'for you'\n        }}\n    ]\n}, {\n    safe: true\n})\n\n// {\n//     'this': [\n//         { contains: 'arrays' },\n//         { preserving: {\n//             them: 'for you'\n//         }}\n//     ]\n// }\n```\n\n### object\n\nWhen enabled, arrays will not be created automatically when calling unflatten, like so:\n\n``` javascript\nunflatten({\n    'hello.you.0': 'ipsum',\n    'hello.you.1': 'lorem',\n    'hello.other.world': 'foo'\n}, { object: true })\n\n// hello: {\n//     you: {\n//         0: 'ipsum',\n//         1: 'lorem',\n//     },\n//     other: { world: 'foo' }\n// }\n```\n\n### overwrite\n\nWhen enabled, existing keys in the unflattened object may be overwritten if they cannot hold a newly encountered nested value:\n\n```javascript\nunflatten({\n    'TRAVIS': 'true',\n    'TRAVIS.DIR': '/home/travis/build/kvz/environmental'\n}, { overwrite: true })\n\n// TRAVIS: {\n//     DIR: '/home/travis/build/kvz/environmental'\n// }\n```\n\nWithout `overwrite` set to `true`, the `TRAVIS` key would already have been set to a string, thus could not accept the nested `DIR` element.\n\nThis only makes sense on ordered arrays, and since we're overwriting data, should be used with care.\n\n\n### maxDepth\n\nMaximum number of nested objects to flatten.\n\n``` javascript\nimport { flatten } from 'flat'\n\nflatten({\n    key1: {\n        keyA: 'valueI'\n    },\n    key2: {\n        keyB: 'valueII'\n    },\n    key3: { a: { b: { c: 2 } } }\n}, { maxDepth: 2 })\n\n// {\n//   'key1.keyA': 'valueI',\n//   'key2.keyB': 'valueII',\n//   'key3.a': { b: { c: 2 } }\n// }\n```\n\n### transformKey\n\nTransform each part of a flat key before and after flattening.\n\n```javascript\nimport { flatten, unflatten } from 'flat'\n\nflatten({\n    key1: {\n        keyA: 'valueI'\n    },\n    key2: {\n        keyB: 'valueII'\n    },\n    key3: { a: { b: { c: 2 } } }\n}, {\n    transformKey: function(key){\n      return '__' + key + '__';\n    }\n})\n\n// {\n//   '__key1__.__keyA__': 'valueI',\n//   '__key2__.__keyB__': 'valueII',\n//   '__key3__.__a__.__b__.__c__': 2\n// }\n\nunflatten({\n      '__key1__.__keyA__': 'valueI',\n      '__key2__.__keyB__': 'valueII',\n      '__key3__.__a__.__b__.__c__': 2\n}, {\n    transformKey: function(key){\n      return key.substring(2, key.length - 2)\n    }\n})\n\n// {\n//     key1: {\n//         keyA: 'valueI'\n//     },\n//     key2: {\n//         keyB: 'valueII'\n//     },\n//     key3: { a: { b: { c: 2 } } }\n// }\n```\n\n## Command Line Usage\n\n`flat` is also available as a command line tool. You can run it with [`npx`](https://docs.npmjs.com/cli/v8/commands/npx):\n\n```sh\nnpx flat foo.json\n```\n\nOr install the `flat` command globally:\n \n```sh\nnpm i -g flat \u0026\u0026 flat foo.json\n```\n\nAccepts a filename as an argument:\n\n```sh\nflat foo.json\n```\n\nAlso accepts JSON on stdin:\n\n```sh\ncat foo.json | flat\n```\n","funding_links":[],"categories":["JavaScript","Object"],"sub_categories":["React Components"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhughsk%2Fflat","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhughsk%2Fflat","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhughsk%2Fflat/lists"}