What does a JS object to JSON converter do?
A JavaScript object and a JSON document look alike, but they are not the same thing. JSON is strict: keys in double quotes, no comments, no trailing commas, no hexadecimal numbers. A JavaScript object literal is much more relaxed. You can write name: 'stdlib' with no quotes on the key and single quotes on the value, leave a comma after the last item, add comments and write 0x1F90 or .5. When you copy a config from a source file, a Stack Overflow answer or a test, JSON.parse rejects it.
This tool converts in both directions. From a JavaScript object to valid JSON, so you can send it to an API or save it as a .json file. From JSON to a JavaScript object, with the quotes, the indent and the declaration you prefer, so you can paste data into code. The syntax read is the one of JSON5, which is a superset of what JavaScript allows inside an object literal.
The important point is how the text is read: as data, never executed. The tool does not call eval and does not run your code. If the text contains new Date(), a function, undefined or a template string, it refuses to convert and says so, because those are code and have no JSON equivalent. That makes it safe for text from sources you do not trust. Everything runs in your browser.
How to convert a JavaScript object to JSON and back
- Choose the direction: "JS object to JSON" or "JSON to JS object".
- Paste the text. In the first direction you can paste the whole declaration, such as
const config = {...};,export default {...}ormodule.exports = {...}. The wrapper and the final semicolon are removed before the text is read. - Choose the indent: 2 spaces, 4 spaces, a tab, or minified for JSON in a single line.
- In the JSON to JS direction, pick the quotes (single or double) and how the result is written:
const name = ...,let,var,export default,module.exportsor the object only. Set the name when the form has one. - Copy the result or download it.
- If the tool reports an error, it gives the line and column of the problem.
Examples
A config copied from a source file
Input:
const config = {
name: 'stdlib',
port: 0x1F90,
tags: ['a', "b",],
ratio: .5, // half
'content-type': 'text/plain',
};JSON:
{
"name": "stdlib",
"port": 8080,
"tags": [
"a",
"b"
],
"ratio": 0.5,
"content-type": "text/plain"
}The hexadecimal number became decimal, .5 became 0.5, the comment and the trailing comma are gone and every key has double quotes.
JSON to a JavaScript object
Input:
{"name":"stdlib","port":8080,"content-type":"x","big":9007199254740993}With single quotes, the name config, const and 2 spaces:
const config = {
name: 'stdlib',
port: 8080,
'content-type': 'x',
big: 9007199254740993n
};Keys that are valid names lose their quotes. content-type keeps them because of the hyphen. The integer is past 2^53, so it is written as a BigInt literal and keeps every digit. With module.exports as the wrapper and {"a":[1,2]} as input, the result is:
module.exports = {
a: [1, 2]
};What it refuses
This is code, not data:
{a: new Date()}The tool answers that it has code in it (new, a function, undefined or a template string) and that only data can be converted. A plain syntax mistake is reported with its position:
{a: 1 b: 2}The error points to line 1, column 7, where the comma is missing. And {a: Infinity} gives {"a": null} with a warning, because JSON has no Infinity or NaN.
Common mistakes
- Expecting code to be evaluated.
{a: 1 + 1},{d: new Date()}and{f: () => 1}are refused. Replace them with the final values first. - Pasting several statements. The tool reads one object. A file with two declarations, or an object followed by other code, is not valid input. Paste only the object you need.
- Leaving out the comma between items. This is the most common syntax error. The line and column help you find it.
- Forgetting that JSON loses some values.
undefinedis not allowed, andInfinityandNaNbecomenull. Dates must already be strings. - Using JSON5 syntax and calling it JSON. The output of the first direction is strict JSON, but the input is not. If you need to keep comments, do not convert.
- Big numbers in a JavaScript object. Past 2^53 a plain number loses precision, which is why the result uses a BigInt literal. Check that your target code supports it, or store the number as a string.
Frequently asked questions
Is my code executed?
No. The text is parsed as data with a JSON5 parser, and anything that would need to run is refused. It is safe to paste text from an unknown source.
Can I paste const x = {...}; with the declaration?
Yes, in the first direction. The wrapper (const, let, var, export default, module.exports) and the closing semicolon are ignored. In the other direction you choose which one to write.
What is the difference between JSON and JSON5?
JSON5 allows what JavaScript allows in an object: unquoted keys, single quotes, comments, trailing commas and hexadecimal numbers. The output of this tool is always strict JSON. To tidy it afterwards, use JSON prettify.
Can I turn the JSON into types?
Yes. Use JSON to code for TypeScript, Go and other languages, or JSON to YAML, XML and CSV to change the format.