Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

nested-object-to-key-value

A lightweight utility to flatten nested JavaScript objects into dot-notation key-value pairs and unflatten them back. Perfect for handling complex configurations, form data, or API transformations.

typescript, javascript, object, flatten, unflatten, key-value, nested-objects, dot-notation, object-transform, deep-object, configuration, form-data, json-transform, object-manipulation, data-structure

readme

🦤 Nested objects to key value

build and test workflow npm version License: MIT TypeScript

A lightweight utility to flatten nested JavaScript objects into dot-notation key-value pairs and unflatten them back. Perfect for handling complex configurations, form data, or API transformations.

Features

  • 🎯 Zero dependencies
  • 📦 Lightweight (~XKB gzipped)
  • 💪 Fully typed (TypeScript)
  • 🔍 Preserves type information
  • ⚡ Tree-shakeable
  • 🧪 Well tested

📦 Installation

npm install nested-object-to-key-value

📚 Usage

import { flattenJson, unflattenJson } from "nested-object-to-key-value";
// Flatten a nested object
const nested = {
user: {
        name: "John",
        address: {
            city: "New York",
            country: "USA",
        },
    },
};
const flattened = flattenJson(nested);
// Result:
// [
//   { key: "user.name", value: "John" },
//   { key: "user.address.city", value: "New York" },
//   { key: "user.address.country", value: "USA" }
// ]

// Unflatten back to nested object
const flat = {
    "user.name": "John",
    "user.address.city": "New York",
    "user.address.country": "USA",
};
const unflattened = unflattenJson(flat);
// Result:
// {
//   user: {
//   name: "John",
//   address: {
//   city: "New York",
//   country: "USA"
// }
// }
// }

📘 API

flattenJson(obj: UnflattenedJson, prefix?: string): FlattenedPair[]

Converts a nested object into an array of flattened key-value pairs.

unflattenJson(obj: Record<string, string>): UnflattenedJson

Converts a flat object with dot-notation keys back into a nested object.

keyValueArrayToObject(obj: FlattenedPair[]): Record<string, string>

Converts an array of key-value pairs into a flat object.

🚲 Testing

We use Vitest for testing.

npm test

🤝 Contributing

Contributions are welcome! Please open an issue or submit a pull request.

📝 License

MIT

changelog

Changelog

All notable changes to this project will be documented in this file.

[1.0.0] - 2025-03-06

Added

  • Initial release
  • Core functionality for flattening and unflattening objects
  • TypeScript support
  • Full test coverage