npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

flatten-to-key-value

v1.1.3

Published

Flatten JSON-like values into dot-path key/value pairs or unFlatten them back into nested objects, with support for arrays, dates, and customizable options.

Downloads

199

Readme

flatten-to-key-value

Flatten or unflatten any JSON-like value.

  • Flatten: turn objects into { "a.b.c": "value", ... }.
  • Unflatten: rebuild nested structures from flattened key/value pairs.
  • Arrays of primitives aggregate into a single key and are joined (default ,), preserving null placeholders.
  • Arrays of objects merge matching leaf paths across elements and rebuild the original array of objects when every key contains the same number of values.
  • Dates are serialized to ISO strings and restored back as Date objects when possible.
  • Optional deduping, key sorting, delimiter and joiner customization.
  • Opt-in preserveArrayStructure keeps numeric indexes in the flattened output when you do not want values to be merged.

Install

npm i flatten-to-key-value
# or
pnpm add flatten-to-key-value
# or
yarn add flatten-to-key-value

Usage

ESM

import { flattenToKeyValue, unflattenKeyValue } from "flatten-to-key-value";

const contacts = [
  {
    firstname: "Jane",
    lastName: "Doe",
    email: "[email protected]",
    phoneNumber: "+61 400 000 000",
  },
  {
    firstname: "Mark",
    lastName: "Smith",
    email: "[email protected]",
    phoneNumber: null,
  },
];

const flat = flattenToKeyValue(contacts);
console.log(flat);
/*
{
  firstname: "Jane,Mark",
  lastName: "Doe,Smith",
  email: "[email protected],[email protected]",
  phoneNumber: "+61 400 000 000,null"
}
*/

const restored = unflattenKeyValue(flat);
console.log(restored);
/*
[
  {
    firstname: "Jane",
    lastName: "Doe",
    email: "[email protected]",
    phoneNumber: "+61 400 000 000"
  },
  {
    firstname: "Mark",
    lastName: "Smith",
    email: "[email protected]",
    phoneNumber: null
  }
]
*/

// Need indexes instead of merged values?
const preserved = flattenToKeyValue(contacts, { preserveArrayStructure: true });

When only some keys contain joined values, unflattenKeyValue leaves them as arrays alongside scalar fields so that no information is lost. Use the options object to tweak the delimiter, joiner, sorting, or to dedupe repeated values before joining.

CommonJS

const {
  flattenToKeyValue,
  unflattenKeyValue,
} = require("flatten-to-key-value");

const out = flattenToKeyValue({ a: { b: [1, 2] } });
const back = unflattenKeyValue(out);

API

type FlattenOptions = {
  delimiter?: string; // default "."
  joiner?: string; // default ","
  includeNullUndefined?: boolean; // default false (skips null/undefined)
  dedupeArrayValues?: boolean; // default false
  sortKeys?: boolean; // default false
  preserveArrayStructure?: boolean; // default false (keeps numeric indexes)
};

function flattenToKeyValue(
  input: unknown,
  opts?: FlattenOptions
): Record<string, string>;

function unflattenKeyValue(
  input: Record<string, string>,
  opts?: FlattenOptions
): unknown;

Notes

  • Flatten

    • Top-level primitives result in an empty object (no anonymous key).
    • Circular references fall back to String(v) for non-serializable values.
    • Arrays of primitives: each element is collected under the same key and joined at the end.
    • Arrays of objects: leaf paths are merged across elements—use dedupeArrayValues: true to remove repeats.
  • Unflatten

    • Keys are split by delimiter to rebuild objects.
    • Joined values are split by joiner back into arrays.
    • If every flattened key splits into the same number of values (> 1), the output is rebuilt as an array of objects in that order.
    • Mixed single/multi-value keys fall back to objects with arrays so scalars stay scalars.
    • String values are parsed into numbers, booleans, null, undefined, Date objects, or JSON where possible.
    • Single-element arrays are collapsed back to scalars.

Development

pnpm i
pnpm test
pnpm build

License

MIT