JSON Key-Value Swapper

Takes a flat JSON object with scalar values and inverts it: each original value (stringified) becomes a key mapping to its original key name. Nested objects/arrays as values are rejected since there's no sensible key to derive from them. A free online tool from Staaarter, right in your browser.

Runs locallyUpdated 2026-07-24

Overview

Introduction

A flat JSON object is really just a set of key-value pairs, and sometimes you need to look things up by value instead of by key.

This tool inverts a flat object so every original value becomes a lookup key, and every original key becomes that entry's value.

What Is JSON Key-Value Swapper?

A key/value inverter for flat JSON objects: it iterates every top-level entry and builds a new object where String(value) is the key and the original key is the value.

It's restricted to flat objects with scalar values on purpose, inverting a nested object or array value has no unambiguous key to use.

How JSON Key-Value Swapper Works

The tool parses the input and checks the top-level value is a plain object (not an array, not null); it then iterates Object.entries(), and for any object or array value, it stops and reports which key had the invalid value.

For scalar values, it calls String(value) to get the new key (so numbers, booleans, and null all become predictable string keys like "30", "true", "null") and maps that to the original key name.

When To Use JSON Key-Value Swapper

Use it when you have a lookup table shaped as key-to-code and need the reverse code-to-key direction instead.

It's also handy for quickly checking whether any two keys in an object accidentally share the same value, since a collision will show up as a missing entry in the swapped output.

Features

Advantages

  • Makes reverse lookups trivial by turning values into keys.
  • Clearly reports which key had a non-scalar value instead of failing silently.
  • Converts non-string scalar values (numbers, booleans, null) into predictable string keys.

Limitations

  • Only works on flat objects with scalar values; nested objects or arrays as values are rejected outright.
  • Duplicate values collide and silently overwrite each other in the output, since JSON objects can't hold two entries with the same key.

Examples

Swapping a status-code lookup

Input

{"ok":200,"notFound":404,"serverError":500}

Output

{
  "200": "ok",
  "404": "notFound",
  "500": "serverError"
}

Each numeric value becomes a string key, mapped back to its original key name.

Rejecting a nested value

Input

{"user":{"id":1}}

Output

Error: Value for key "user" is an object/array; swap-keys-values only supports flat objects with scalar values.

Nested objects have no single value to use as a key, so the tool reports exactly which key failed.

Best Practices & Notes

Best Practices

  • Check your input for duplicate values first if the mapping needs to be lossless in both directions.
  • Flatten a nested object first with JSON Object Flattener if you need to swap something that isn't already flat.
  • Use this for small lookup tables (enums, status codes, short mappings), not large datasets where value collisions are likely.

Developer Notes

String(value) is used rather than JSON.stringify(value) to build the new key, so a numeric value 200 becomes the key "200" rather than the JSON-quoted "200" wrapped again; this keeps the output's keys clean and directly usable rather than double-encoded.

JSON Key-Value Swapper Use Cases

  • Inverting a status-code-to-name or error-code-to-name lookup table
  • Building a reverse index from a simple id-to-label mapping
  • Spotting accidental duplicate values in a flat config object

Common Mistakes

  • Running this on a deeply nested object and being surprised by the rejection; flatten it first if that's genuinely what you need.
  • Not checking for duplicate values beforehand and losing entries silently when two keys map to the same value.

Tips

  • If the swap fails, the error names the exact key with the non-scalar value so you know where to fix the input.
  • Combine with Extract JSON Values if you only need the list of values, not a full inverted mapping.

References

Frequently Asked Questions