Skip to main content
CalcChef

JS Object to JSON Converter

Paste a JavaScript object or array literal and get JSON. Unquoted keys, single quotes and trailing commas are accepted, and no code is run. How it works

Use the JS Object to JSON Converter

JSON Output

{
  "name": "CalcChef",
  "features": [
    "Fast",
    "Clean"
  ],
  "active": true
}

About the JS Object to JSON Converter

The object { name: 'Ada', tags: ['a', 'b',], active: true, } becomes JSON with double-quoted keys and strings, no trailing comma and a 2-space indent (or 4 spaces, a tab or one compact line). This converter reads a JavaScript object or array literal with a restricted parser that never runs code, then prints it with JSON.stringify. It works one way, from JavaScript literal text to JSON text.

What the converter does with common inputs

InputOutput (shown compact here) or error
{ name: 'Ada' }{"name":"Ada"}
{ 'a-b': "x", }{"a-b":"x"}
[1, 2, 3,][1,2,3]
{ n: +1, m: 1e3 }{"n":1,"m":1000}
{ id: 12345678901234567890 }{"id":12345678901234567000}
{ '2': 'b', a: 'c', '1': 'd' }{"1":"d","2":"b","a":"c"}
{ a: 1, a: 2 }{"a":2}
{ a }Failed to convert JS to JSON: Expected ':' at line 1, column 5 (position 4)
{ a: undefined }Failed to convert JS to JSON: undefined has no JSON form at line 1, column 6 (position 5)
{ 1: 'a' }Failed to convert JS to JSON: A number used as a key must be in quotes at line 1, column 3 (position 2)
{ when: new Date() }Failed to convert JS to JSON: Expected an object, array, string, boolean, number, or null at line 1, column 9 (position 8)
{ café: 1 }Failed to convert JS to JSON: Expected ':' at line 1, column 6 (position 5)
{ p: .5 }{"p":0.5}

Each cell is what the tool returns for that input, shown here as the Compact (one line) setting; the page starts at a 2-space indent. Line and column start at 1, position at 0.

How to use this tool

  1. Paste a JavaScript object or array literal into the box. A small starter object is there when the page opens.
  2. Read the JSON below it, indented with 2 spaces; the Indent menu switches to 4 spaces, a tab or Compact (one line). An empty box shows nothing to convert.
  3. If the text cannot be read, the message gives the reason and the line, column and position of the problem.
  4. Press Copy for the JSON, or Share for a link that carries your text and any indent other than 2 spaces (up to 10,000 characters are read back from a link).

Frequently asked questions

How do I convert a JavaScript object to JSON?
Paste the object as text. The tool accepts unquoted keys, single-quoted strings and trailing commas, which JSON does not, and prints valid JSON. If the object is a live value in your own code, JSON.stringify(value, null, 2) gives the same text without pasting.
Does it run my JavaScript?
No. A restricted parser builds the value from literals only. A function call, new Date(), a property access, a template string or a comment is rejected with an error, so nothing in the box can run. The conversion happens in the page; Share, if you press it, puts your text in a link.
Why does 12345678901234567890 come out as 12345678901234567000?
Numbers are read as JavaScript doubles, which hold whole numbers exactly only up to 9,007,199,254,740,991. RFC 8259 says integers from -(2^53)+1 to 2^53-1 are the ones software can exchange reliably. The JSON Validator & Auto-Repair tool keeps the digits of a number exactly as typed.
Why are my keys in a different order?
Keys that look like whole numbers come first, in ascending order, as in any JavaScript object. { '2': 'b', a: 'c', '1': 'd' } prints 1, 2, a. Other keys keep the order you wrote. When a key appears twice, the last value wins.
Should I use this or the JSON Validator & Auto-Repair?
Use this one for an object literal you want rebuilt as clean JSON: it parses the value, indents it the way you choose, reads numbers as doubles and moves integer-like keys to the front. Use the Auto-Repair tool for text that is almost JSON and should stay as written: it adds the missing quotes and drops trailing commas and comments in place, so { b: 1, '2': 'x', n: 12345678901234567890, } keeps its key order and all 20 digits.
What do line, column and position mean in the error?
Line and column start at 1 and count in UTF-16 code units; position is the 0-based offset from the start of the text. For { a } the message is Expected ':' at line 1, column 5 (position 4): a shorthand property has a name but no colon and value.

Related Tools

View all tools