JSON Wrench

JavaScript Object to JSON

Input
All processing happens in your browser. Your JSON is never uploaded.
Output
Paste JSON on the left. It's checked as you type. Broken JSON gets an exact error location and a one-click fix.

A JavaScript object literal copied from source code, a test fixture or the browser console looks like JSON but is not: keys are unquoted, strings use single quotes, and there are trailing commas. Paste it above and press Fix to get valid JSON.

You can even paste a whole statement such as const user = { id: 7 };. The text before and after the object is removed.

Example

Input
{
  id: 7,
  name: 'Ada',
  tags: ['math', 'code',],
  nickname: undefined,
  score: .5,
}
Result in JSON Wrench
{
  "id": 7,
  "name": "Ada",
  "tags": [
    "math",
    "code"
  ],
  "nickname": null,
  "score": 0.5
}

What gets converted

  • Unquoted keys like id: become "id":.
  • Single-quoted strings become double-quoted.
  • Trailing commas after the last item are removed.
  • // and /* */ comments are removed.
  • undefined, NaN and Infinity become null, because JSON has no equivalent.
  • Numbers like .5, +5 and 0x1F become 0.5, 5 and 31.
  • Code around the object, such as const user = and the final ;, is removed.

What it cannot convert

JSON holds data, not code. Functions, class instances, template literals with ${} expressions, Date objects and variables referenced by name cannot be turned into meaningful JSON. Evaluate the object in JavaScript and serialize it instead.

The reliable way in code

If you can run the code, JSON.stringify(obj, null, 2) produces correct JSON. Note that it drops keys whose value is undefined or a function, and turns NaN and Infinity into null. Use this page when you only have the source text, for example from a code review, a docs page or a chat message.

Frequently asked questions

What is the difference between a JavaScript object and JSON?

JSON is a strict text format inspired by JavaScript object syntax. It requires double-quoted keys and strings, allows no trailing commas or comments, and supports only strings, numbers, booleans, null, objects and arrays.

What happens to undefined?

Auto-fix replaces undefined with null so the key is kept. JSON.stringify would drop the key instead.

More JSON tools