Help

JavaScript Object to JSON Converter: Loose Syntax to Strict JSON

Paste a JavaScript object with unquoted keys, single quotes, comments or a trailing comma and get back strict, escaped JSON, minified or indented. It is read by a parser and never run as code, so you can paste text from a console, a config file or a code sample.

How to convert a JavaScript object to JSON

  1. Paste the object into the JavaScript object or JSON box (it says Paste a JavaScript object or loose JSON here...). A paste is converted at once. When you type, the result updates about 300 milliseconds after you stop.
  2. Choose how the result is laid out in Format: Minified (the default, a single line), 2 spaces or 4 spaces.
  3. Read the result under Strict JSON. Converted to strict JSON. and a character count appear beneath it.
  4. Select Copy, directly under the result. The button reads Copied for a moment.
  5. Select Clear to empty the box and start again.

Here is a small example. This is what you paste:

Input {
  name: 'Ana',
  tags: ['a', 'b',], // trailing comma
  id: 0xFF,
}

With Minified selected, the result is:

Strict JSON {"name":"Ana","tags":["a","b"],"id":255}

What loose JavaScript syntax it accepts

The text is read with the JSON5 parser, which understands the ways a JavaScript object literal differs from JSON. Each of these is converted rather than rejected:

What you pasteExampleIn the result
Unquoted key{name: "Ana"}{"name":"Ana"}
Single-quoted string{a: 'x'}{"a":"x"}
Trailing comma[1, 2,][1,2]
Comments// note and /* note */removed
Hex number0xFF255
Leading plus or bare decimal point+1, .5, 5.1, 0.5, 5
NaN and Infinity{a: Infinity}{"a":null}
String split across lines with a backslash'one \ then a line breakone string, with no line break

Quotes, backslashes and line breaks inside a string are escaped for you, so 'He said "hi"' arrives as "He said \"hi\"". Anything that is already valid JSON passes through unchanged apart from the layout you chose.

Minified, 2 spaces or 4 spaces

The Format menu sits above the result. Minified writes everything on one line with no spaces, which suits a request body, a fixture or a value to paste into a string. 2 spaces and 4 spaces give an indented layout that is easier to read and diff. The result changes as soon as you choose, and Copy takes whatever you see.

What it cannot read, and the error message

Only data is accepted, not code. When the parser stops, a red Could not read this box gives a plain reason and Line N, column M. For example, {a:1 b:2} stops at the b, and the box says The parser stopped at “b”. Check for a missing comma, colon, bracket or quote just before it. at Line 1, column 6. If the text finishes while a bracket, a brace or a quote is still open, you get The text ends too early. instead.

If you pasteWhat happens
const x = {a: 1}Stops at the first letter. Paste only the object.
{a: 1};Stops at the final semicolon. Remove it.
A backtick (template) stringNot accepted. Use single or double quotes.
undefined, a function or new Date()Not accepted. They are code or have no JSON form; write a date as a string.
A missing comma, colon or bracketStops at the character after the gap, with its line and column.

Numbers, duplicate keys and other things that change

The result is produced by the browser's own JSON.stringify, so numbers are JavaScript numbers. That has consequences worth knowing before you copy:

If the data is already valid JSON and you only want it indented with every digit kept exactly, use the JSON Formatter. To check JSON against a schema, use the JSON Schema Validator.

Limits and privacy

Frequently asked questions

How do I convert a JavaScript object to JSON online?

Paste the object into the JavaScript object or JSON box. It is converted at once on paste, or about 300 milliseconds after you stop typing, and the strict JSON appears under Strict JSON. Select Copy to take it with you. Paste only the object itself, not a line such as const data = {...};.

How do I convert single quotes to double quotes in JSON?

Paste the text. Every single-quoted string comes out double-quoted, and a double quote inside it is escaped for you, so 'He said "hi"' becomes "He said \"hi\"". Only strings and keys are touched, so an apostrophe inside a double-quoted string is left alone.

How do I remove a trailing comma from JSON?

Paste the text. A comma after the last item of an object or array, such as [1, 2,], is accepted on input and left out of the Strict JSON result. Nothing else about the data changes.

Can JSON have unquoted keys or comments?

Strict JSON cannot: every key and string needs double quotes, and there is no comment syntax. JavaScript objects and config files often have both. This tool reads them, then writes the keys with double quotes and drops the comments, so the result is valid JSON.

What is the difference between JSON and JSON5?

JSON5 is a looser superset of JSON that allows unquoted keys, single-quoted strings, trailing commas, comments, hex numbers and a few more JavaScript-style forms. This tool reads that syntax and writes plain JSON, so any JSON5 document without unsupported values converts to JSON that every parser accepts.

How do I minify JSON or indent it with 2 or 4 spaces?

Use the Format menu above the result. Minified, the default, gives one line with no spaces. 2 spaces and 4 spaces give an indented version. The result and what Copy takes both change as soon as you pick another option.

Why does it say Could not read this?

The text is not an object, array or value that the parser understands. The red box says where it stopped, for example Line 1, column 6, and what to check: a missing comma, colon, bracket or quote just before that point, or a bracket, brace or quote that is still open at the end. Statements such as const x = {...} or a trailing semicolon, backtick strings, undefined, functions and new Date() are not accepted. Paste just the object.

Is the JavaScript I paste executed?

No. The text is read by a parser (JSON5), never by eval, so nothing you paste can run. That is also why code is rejected: a function, a variable name or new Date() is an error, not something to evaluate.

Why did my big number change?

Numbers become JavaScript numbers, which are exact only up to 9007199254740991. 12345678901234567890 comes out as 12345678901234567000, and 1.0 comes out as 1. If you need every digit of a long ID kept, put it in quotes in your input, or use the JSON Formatter, which copies numbers exactly as written.

What happens to NaN, Infinity and duplicate keys?

NaN, Infinity and -Infinity become null, as JSON.stringify does, because JSON has no way to write them. If a key appears twice, the last value wins and the earlier one is dropped.

How do I escape quotes and newlines in a JSON string?

You do not have to. A quote, a backslash or a line break inside a string is written as \", \\ or \n in the result. This tool does not wrap the whole JSON into one escaped string to embed in another string; it gives you the JSON itself.

Is this the same as JSON.stringify?

The result is made with JSON.stringify, so the output rules are the same. The difference is the input: JSON.stringify needs a real object in your code, while here you paste the text of the object, including loose syntax. It does not do the reverse of JSON.parse; it reads loose syntax and always writes strict JSON.

How large can the text be, and is it uploaded?

Up to 5,000,000 characters. The conversion happens on your device and nothing is uploaded or saved.

Recommended

Helps keep the tools free. ✨

Images are for illustration only. As an Amazon Associate I earn from qualifying purchases.