Skip to the content
GigaJSON
All tools
Format and check
View and query
Convert
JSON to code
Encode and decode
Sample files
Guides Open GigaJSON

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, false and null are lowercase, without quotes. A number has no quotes either, unless the program expects text.
  • No comments. // note and /* 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:

  1. Open the file, or paste the JSON. It's read in your browser and not uploaded.
  2. Double-click a value or a key to change it. The row buttons add, duplicate and delete entries.
  3. Switch to Code to edit the text directly, with error marks like VS Code's.
  4. 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.

Questions

How do I edit a JSON file?

Open it in any text editor (Notepad, TextEdit in plain text mode, VS Code), change the value between the quotes and save. Keep the punctuation as it was: double quotes around text, a comma between items but not after the last one, and no comments. An online JSON editor lets you change values in a tree instead, so the punctuation can't break.

What is the best way to edit a JSON file?

For a quick change, a code editor such as VS Code, which marks mistakes as you type. For many records at once, a script with jq or Python. For a large file, or when you'd rather not touch the syntax, a JSON editor with a tree and a table view, such as GigaJSON's, which also keeps undo and version history.

Can I edit a JSON file with jq?

Yes, but jq writes the result to standard output; it never changes the file itself. Write to a temporary file and move it over the original: jq '.port = 9090' config.json > config.tmp && mv config.tmp config.json. Redirecting straight to config.json empties it.

Why does my JSON file stop working after I edit it?

Usually a trailing comma after the last item, a missing comma between two items, a comment, single quotes, or curly quotes added by a word processor or TextEdit. A JSON validator gives the line and column of the first problem.

Can I edit a large JSON file?

Text editors slow down past a few tens of megabytes; VS Code turns off validation and folding for large files. GigaJSON's JSON editor opens files up to 1 GB in the browser, edits them in a tree or table, and saves each change without rewriting the whole file.

More from the blog