EditSafely

Swap JSON Keys with Values

Exchange keys with values throughout a JSON file.

Runs on your device, nothing uploaded

When values repeat
Indent
Try:

0 chars · 0 lines

Output

Exchange keys with values throughout a JSON file.

Type or paste above to see the result here.

Example: Invert a lookup object

What you paste never leaves this tab

Swap JSON Keys with Values is JavaScript that your browser has already downloaded, and it does the whole job on your own machine. On a normal site, an API response with live access tokens in it is sent to a server that can log it, cache it or keep it. Here it is read and transformed inside this tab and nowhere else.

  • No upload
  • No length limit
  • Works offline once loaded
  • Nothing stored or logged

How to use Swap JSON Keys with Values

  1. 1. Paste your JSON object. Put a flat JSON object of key-value pairs into the input pane. Each existing key becomes a value and each existing value becomes the new key in the output.
  2. 2. Decide what happens when values repeat. If several keys share a value, only one can become the new key. Keep last or Keep first picks one and the warning above the result names what was lost; Collect into a list keeps every original key in an array. Pick an indent to match your project's style.
  3. 3. Copy the inverted object. Copy the swapped result and paste it wherever you need to look up the original keys by their values instead, such as a reverse lookup table in your code.

When to use Swap JSON Keys with Values

Swap JSON Keys with Values is for turning a forward lookup object into a reverse one. Any time you have a JSON map from names to codes, or codes to labels, and need to search it in the other direction, this exchanges every pair for you.

  • Building a reverse enum lookup. An app has a JSON map from status codes to human-readable labels, but a new feature needs to go from label back to code. Swap the object instead of writing a second map by hand.
  • Inverting a locale ID table. A localization system maps locale codes to display names, and a form needs the opposite direction to convert a user's selected language name back into a code.
  • Creating a reverse index. A JSON object maps usernames to user IDs for one part of a system, but another part needs to find the username given only the ID. Swapping produces that index instantly.
  • Flipping a role assignment file. A JSON file maps roles like admin and editor to the person holding them, and you need a quick way to look up which role a given person currently holds.

Examples

Invert a lookup object

Input

{"admin": "alice", "editor": "bob"}

Output

{
  "alice": "admin",
  "bob": "editor"
}

Collect keys that share a value

Input

{"ada": "admin", "lin": "editor", "sam": "admin"}

Output

{
  "admin": [
    "ada",
    "sam"
  ],
  "editor": [
    "lin"
  ]
}

About the Swap JSON Keys with Values tool

Swap JSON Keys with Values does its work locally, right in the browser. Exchange keys with values throughout a JSON file. There is no upload step, no queue and no account, and your data never travels over the network.

It belongs to the JSON Tools collection on EditSafely, a set of 90 small, focused JSON utilities that share the same instant, private workspace.

You can shape the output with 2 settings, including When values repeat and Indent, and the result refreshes the moment you change one. 2 worked examples further down the page show exactly what the tool produces for real inputs.

Running locally also makes the tool fast and dependable: results appear as soon as your input does, there is no server outage that can take it down mid-task, and confidential data can be processed without a second thought.

Frequently asked questions

Is Swap JSON Keys with Values free to use?

Yes, it is completely free. All 2,943 tools on EditSafely work without an account, a subscription or usage limits.

Is it safe to paste sensitive or confidential data?

Everything happens locally. Your browser downloads the tool's code once, then does all the processing itself; nothing you enter is transmitted, stored or logged. You can even go offline after the page loads and it will still work.

How much text can I process at once?

There is no fixed limit; your device's memory is the ceiling. Up to 10 MB the result updates as you type, after a short pause that grows with the size. Above 10 MB a Run button appears so typing stays smooth, and files open up to 50 MB. A very long result shows its first 2,000 lines on screen, while Copy and Download always include all of it.

Do I need to sign up or install anything?

No. The tool works in any modern browser on desktop, tablet or phone. There is no account to create, no extension to add and no software to install.

How do I use the result?

The output panel has a one-click copy button, and you can keep refining the input while you work; the result updates in place as you type.

Related tools

All JSON Tools →