JSON QuickFix › Convert a JavaScript object to JSON

Convert a JavaScript object to JSON

Copying an object out of JavaScript or TypeScript code gives you {id: 1, name: 'x'}, which JSON parsers reject because keys must be quoted. Paste it below and Quick Fix quotes every key and cleans up the rest.

Input

Paste some JSON, drop a .json file, or load the sample to get started.
Advertisement

JSON is stricter than JavaScript

JSON borrowed its syntax from JavaScript but removed the flexible parts. An object literal that runs fine in a browser can contain several things JSON forbids:

  • Unquoted keys: {id: 1} must be {"id": 1}
  • Single-quoted strings
  • Comments (// and /* */)
  • Trailing commas
  • undefined, NaN and Infinity, which have no JSON form

Using JSON.stringify

If the object is available at runtime, JSON.stringify(obj, null, 2) in the browser console gives you JSON directly (properties set to undefined are dropped). Use Quick Fix when you only have the source text, such as a snippet from a file, documentation or a code review.

What Quick Fix can't convert

Functions, template literals, variables and computed keys are code, not data. Quick Fix will report their position so you can replace them with literal values.

Advertisement

Frequently asked questions

Why do JSON keys need quotes?

The JSON specification defines object keys as strings, and strings must be in double quotes. Unquoted keys are valid JavaScript but not valid JSON.

What does undefined become?

Quick Fix replaces undefined, NaN and Infinity with null, the closest JSON value, and lists each replacement so you can review it.

Does it work with TypeScript objects?

Yes for plain object literals. Type annotations and 'as' casts are code rather than data and need to be removed first.