How to edit a JSON file without breaking it
· 6 min read · By Amir Badi
A JSON file is plain text, so any editor can change it. The hard part is keeping it valid: one comma in the wrong place and the program that reads it refuses to start. This guide covers editing by hand, in VS Code, in an online editor, and with jq or Python when the change is too big or too repetitive to do by hand.
I ran every command below with jq 1.7, Python 3.12 and Node 24 on this config.json:
{
"name": "shop-api",
"port": 8080,
"debug": false,
"db": { "host": "localhost", "pool": 5 },
"features": ["search", "export"]
}
Which way to edit
| You want to | Use |
|---|---|
| Change one or two values | A text editor or VS Code |
| Avoid touching the syntax at all | An online JSON editor with a tree view |
| Change the same field in many records | The editor's Table, or jq or Python |
| Change a file in a script or CI job | jq or Python |
| Edit a file of 50 MB or more | A JSON editor made for large files |
In a text editor
Open the file (on Windows: right-click, Open with, Notepad), change the text and save. Before you save, check the rules JSON is strict about:
- Text goes in double quotes:
"localhost", never'localhost'. - A comma goes between items, and never after the last one.
"pool": 5,}is invalid. true,falseandnullare lowercase, without quotes. A number has no quotes either, unless the program expects text.- No comments.
// noteand/* note */aren't JSON, even though some config files allow them (those are JSONC or JSON5).
On a Mac, TextEdit can swap your straight quotes for curly ones (“port”), which no JSON parser accepts. Use Format > Make Plain Text and turn off Edit > Substitutions > Smart Quotes before you edit, or use a code editor. Word processors do the same thing, so don't edit JSON in Word or Google Docs.
In VS Code
VS Code knows JSON: it colours it, underlines a missing comma or quote as you type, and lists every problem in the Problems panel. Format Document (Shift+Alt+F on Windows, Shift+Option+F on a Mac, Ctrl+Shift+I on Linux) re-indents the file after a messy edit. For package.json, tsconfig.json and other files with a known schema, it also suggests keys and flags wrong values.
Large files are where it struggles. VS Code's source turns off tokenization and folding at 20 MB or 300,000 lines, and past 50 MB it stops passing the file to extensions, so the JSON checks go too.
Online, in a tree
A tree editor shows each key and value as a row, so you change values without typing any punctuation. In GigaJSON's JSON editor:
- Open the file, or paste the JSON. It's read in your browser and not uploaded.
- Double-click a value or a key to change it. The row buttons add, duplicate and delete entries.
- Switch to Code to edit the text directly, with error marks like VS Code's.
- Export or download the result as JSON, minified or indented.
Every edit is one undo step (200 of them), and History keeps restore points such as the original file, so you can compare and go back. For the same change across many records, the Table tab shows a list of objects as a grid you can sort, filter and edit, and Find can replace text across the document. It opens files up to 1 GB; the editor's page lists the limits.
With jq
jq is the usual tool for editing JSON in a terminal or a script. Each command prints the changed document:
jq '.port = 9090' config.json # change a value
jq '.db.pool = 20' config.json # a nested value
jq '.timeout = 30' config.json # add a key (it goes last)
jq 'del(.debug)' config.json # delete a key
jq '.features += ["import"]' config.json # append to a list
jq 'with_entries(if .key == "db" then .key = "database" else . end)' config.json # rename a key, same place
To put a value from your shell into the file, pass it as a variable instead of pasting it into the filter. --arg gives a string and --argjson a number, boolean or any JSON:
jq --arg host "$DB_HOST" '.db.host = $host' config.json
jq --argjson pool 12 '.db.pool = $pool' config.json
For a list of records, map changes each one. With this orders.json:
[
{ "id": 1, "status": "paid", "total": 40 },
{ "id": 2, "status": "pending", "total": 15 },
{ "id": 3, "status": "paid", "total": 72 }
]
jq 'map(if .status == "pending" then .status = "cancelled" else . end)' orders.json
jq 'map(.status |= ascii_upcase)' orders.json
jq never changes the file itself. This is the mistake everyone makes once:
jq '.port = 9090' config.json > config.json # don't: config.json is now empty
The shell empties config.json before jq gets to read it. Write to a temporary file and move it over the original:
jq '.port = 9090' config.json > config.tmp && mv config.tmp config.json
&& matters here: if the filter has a typo, jq exits with an error and the original stays as it was. (sponge from moreutils does the same: jq '…' config.json | sponge config.json.)
jq also rewrites the layout. Our one-line "db": { … } comes back spread over four lines, indented by two spaces. Use --indent 4 or --tab to match your file, and expect a bigger diff than the change you made.
With Python
Python's json module reads the file into dicts and lists, and you write it back after changing them:
import json
with open("config.json", encoding="utf-8") as f:
config = json.load(f)
config["port"] = 9090
config["db"]["pool"] = 20
config.setdefault("timeout", 30) # add only if missing
config.pop("debug", None) # delete if present
config["features"].append("import")
with open("config.json", "w", encoding="utf-8") as f:
json.dump(config, f, indent=2, ensure_ascii=False)
f.write("\n")
Keys keep their order and new ones go last. ensure_ascii=False writes é as é instead of é, and the final \n keeps editors and git from complaining about a missing newline. Reading the whole file into memory is fine up to a few hundred megabytes; past that, see how to open a large JSON file.
When the file won't load after an edit
Parsers report the first problem with a position. A comment left in the file gives these messages:
| Tool | Message |
|---|---|
| Python | Expecting property name enclosed in double quotes: line 2 column 3 |
| Node, Chrome | Expected property name or '}' in JSON at position 4 (line 2 column 3) |
| jq | parse error: Invalid numeric literal at line 2, column 5 |
Curly quotes from TextEdit or Word give the same messages, which is why they're hard to spot: the quote looks right on screen. Paste the file into the JSON validator to see the exact spot, or into JSON Repair, which fixes trailing commas, comments, single and curly quotes and lists each change. How to fix invalid JSON explains the ten most common errors one by one.
Editing a large file
A 300 MB export is a different job. Notepad and TextEdit take minutes to open it, VS Code drops its JSON features, and a Python script needs several times the file's size in memory.
GigaJSON's JSON editor is built for this case. It opens files up to 1 GB, parses them in a background worker and draws only the rows on screen, so the tree and the table stay quick. A change is saved as a small log entry instead of rewriting the whole file. Past about 60 MB of compact JSON, the Code tab shows the whole text read-only, and Edit selected part opens the node you picked in the tree in the code editor. When you're done, export the whole document as JSON.
If you only need to look, not edit, the Large JSON Viewer is lighter. And if a file is too big for the program that reads it, the JSON Splitter cuts it into smaller ones.