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

iterable-to-json

v0.2.0

Published

Convert any iterable with dot notation keys into a deeply-nested JSON value.

Readme

Iterable to JSON

Convert any key/value iterable into a deeply-nested JSON value.

Why?

Many modern web APIs use iterables of [key, value] pairs:

  • new FormData(request)
  • new URLSearchParams(location.search)
  • Object.entries(obj) / new Map(entries)

Yet once you have those pairs, turning them into the structured object you actually need is surprisingly fiddly:

  • Keys may describe deeply-nested objects (user.address.city)
  • They often mix arrays and objects (items[3].price)
  • There can be multiple values for the same key (tags=js&tags=ts)
  • Sparse indices (array[8]=foo) must be preserved

iterable-to-json does all of that in one tiny function. Give it any key/value iterable and get back JSON.


Installation

npm install iterable-to-json
# or
pnpm add iterable-to-json
# or
yarn add iterable-to-json

Quick start

import { iterableToJson } from 'iterable-to-json';

const formData = new FormData();
formData.append('user.name', 'Ada');
formData.append('user.hobbies[0]', 'Computation');
formData.append('user.hobbies[1]', 'Flying');
formData.append('flags', 'true'); // multiple entries…
formData.append('flags', 'false'); // …become arrays

const json = iterableToJson(formData);

console.log(json);
/*
{
  user: {
    name: 'Ada',
    hobbies: ['Computation', 'Flying']
  },
  flags: ['true', 'false']
}
*/

Key syntax

| Pattern | Produces | | --------------------------------------- | --------------------------------------- | | user.name = "Ada" | { user: { name: "Ada" } } | | items[0] = "foo" / items[1] = "bar" | { items: ["foo", "bar"] } | | matrix[2][4] = "x" | { matrix: [ , , [ , , , , "x" ] ] } | | meta[0].key.value = "v" (mixed) | { meta: [ { key: { value: "v" } } ] } | | [0].key = "value" (top-level array) | [ { key: "value" } ] | | Duplicate keys (tags=js&tags=ts) | { tags: ["js", "ts"] } |

  • Dot notation (.) → object keys
  • Bracket notation ([index]) → array indices
  • Numeric keys without brackets (e.g. obj.0) are treated as object keys, not arrays — mirroring JSON semantics.
  • Missing indices create undefined "holes" so sparse arrays keep their correct positions.

Decoding rules

iterableToJson only operates on the keys of the iterable. Values are left exactly as received.

Example

import { iterableToJson } from 'iterable-to-json';

const params = new URLSearchParams('active=true');
const raw = iterableToJson(params);

/*
raw === {
  active: 'true' // still a string
}
*/

You should almost always validate data received from FormData or URLSearchParams. As such, you should rely on a validation library such as Valibot or Zod to coerce values if needed.

Example coercion with Valibot

import { v } from 'valibot';

const schema = v.object({
	active: v.union([
		v.boolean(), // accept booleans
		v.pipe(
			v.union([v.literal('true'), v.literal('false')]),
			v.transform((value) => value === 'true'),
		), // accept strings and coerce to booleans
	]),
});

const typed = v.parse(schema, raw); // { active: true }

Example coercion with plain JavaScript

const coerced = { active: raw.active === 'true' };

API

function iterableToJson<T = unknown>(
	iterable: Iterable<[string, T]>,
): DecodedValue<T>;

DecodedValue<T>

A distributive conditional type that maps the decoded structure to the union of possible leaf values T. For most apps you can ignore it – IntelliSense will "just know".

Supported environments

Works everywhere (that you can use a for...of loop).

Feedback

Please open an issue if you find a bug or have a feature request.

License

MIT