JavaScript object to JSON converter
The object is parsed as text and never executed, so you can safely paste code from any source.
Loading the tool…
Processing and privacy: The result is calculated inside your browser and your data is not sent to any server.
How to use it
- Enter javascript object and indent.
- The result appears straight away and updates as you edit.
- Copy the result, or copy a link that keeps the same values to share it.
How it is calculated
Unquoted keys become strings, numeric keys are written the way JavaScript converts them (0x10 becomes "16"), and single-quoted strings and ` templates without substitutions become JSON strings. Hex, binary and octal numbers become decimals, and comments and trailing commas are dropped.
Example
Inputs: Indent = 2 spaces. Result: JSON: { "name": "Ali", "age": 30, "tags": [ "js", "json" ], "active": true }; Values converted: 5.
const user = {
name: 'Ali',
age: 30,
tags: ['js', "json"],
// trailing commas and comments are fine
active: true,
};{
"name": "Ali",
"age": 30,
"tags": [
"js",
"json"
],
"active": true
}Limits and caveats
- undefined, NaN, Infinity, functions and variables have no JSON equivalent and give an error.
- Computed [key] properties, shorthand properties and spread syntax are not supported.
Questions about this tool
Why not use eval or JSON.parse?
JSON.parse rejects unquoted keys, and eval runs the code, which can be dangerous. Here the object is only parsed as text.
Does it accept const data = { };?
Yes. It skips const, let, var, export default, module.exports and the final semicolon.
What happens to duplicate keys?
The last value wins and the key keeps its first position, as in JavaScript.
