JS to JSON Converter
A VS Code extension that converts JavaScript object literals into valid JSON with a single command.
What it does
Turns this:
{
name: 'John',
age: 30,
active: true,
address: {
city: 'Berlin',
zip: '10115',
},
tags: ['admin', 'user'],
}
Into this:
{
"name": "John",
"age": 30,
"active": true,
"address": {
"city": "Berlin",
"zip": "10115"
},
"tags": [
"admin",
"user"
]
}
Specifically it handles:
- Unquoted keys →
"quoted keys"
- Single-quoted strings →
"double-quoted strings"
- Trailing commas (on objects and arrays)
- Nested objects and arrays
- Numbers, booleans,
null
Note: Values that have no JSON equivalent — functions, undefined, class instances — cannot be converted and will show an error.
Usage
Option 1 — Convert a selection
- Select the JS object text in the editor
- Use any of the triggers below — the selection is replaced with valid JSON
Option 2 — Convert the entire file
- Make sure nothing is selected
- Use any of the triggers below — the entire file contents are replaced with valid JSON
Triggers
| Method |
Shortcut |
| Keyboard shortcut |
Cmd+Shift+J (Mac) / Ctrl+Shift+J (Windows/Linux) |
| Right-click context menu |
JS to JSON: Convert Selection |
| Command Palette |
Cmd+Shift+P → type JS to JSON |
Examples
Simple object
Input (selected):
{ id: 1, status: 'active' }
Output:
{
"id": 1,
"status": "active"
}
Nested object with trailing commas
Input (selected):
{
user: {
name: 'Alice',
roles: ['admin', 'editor',],
},
}
Output:
{
"user": {
"name": "Alice",
"roles": [
"admin",
"editor"
]
}
}
Array of objects
Input (selected):
[
{ id: 1, label: 'First' },
{ id: 2, label: 'Second' },
]
Output:
[
{ "id": 1, "label": "First" },
{ "id": 2, "label": "Second" }
]
Error cases
If the selection cannot be parsed as a JS object, an error message appears at the bottom of the screen explaining what went wrong. The editor content is not modified.
Common reasons for failure:
- The text contains a function value:
{ fn: () => {} }
- The text is not a valid JS expression at all
- The text contains
undefined values
Installation
From the Marketplace
Search for JS to JSON Converter in the VS Code Extensions panel (Cmd+Shift+X) and click Install.
From a .vsix file
code --install-extension js-to-json-0.0.1.vsix
Or via VS Code: Extensions panel → ... menu (top-right) → Install from VSIX...