how to edit json files in linux terminal

How to Create and Edit JSON Files in Linux Terminal

If you click our links and make a purchase, we may earn an affiliate commission. Learn more

JSON files are widely used by developers to save configurations and share data across servers and applications. They’re built to be readable by both machines and people. But if you only have a Linux terminal, they’re not that easy to work with. Let me give you a few tips to improve that.

As a whole, JSON files are basically text files formatted in a specific way. Any text editor can create and edit them without any add-on required. But tools like “jq” can be used to make them more readable.

I know that there is no perfect solution to edit JSON files like in a desktop environment, but stay with me as I have some good tips to share with you.

If you need help with Linux, I’ve got something that can help you right away!
Download my free Linux commands cheat sheet – it’s a quick reference guide with all the essential commands you’ll need to get things done on your system. Click here to get it for free!

Creating a JSON File in Linux Terminal

Basically, JSON files are simply text files formatted in a very specific way. While the formatting is strict, the content itself is just text, so you can use any text editor you like for this task.

As JSON files are purely text files ending with a “.json” extension, you can simply create an empty JSON file with the Linux terminal by using the “touch” command:
touch myfile.json

The “touch” command on Linux is widely used to create empty text files. But if your goal is to write the JSON file content, then it’s better to use a full editor. The two most popular are:

  • Nano: It’s now the default text editor installed on most distributions, so you probably already have it. You can create or open an existing JSON file with:
    nano myfile.json
    You can start writing immediately, but you may need a bit of time to understand the shortcuts available if it’s your first time (I have a guide here for you if needed).
    editing json files in nano
  • Vim: Another popular alternative to Nano. If you’re used to it, you may want to try it first.
    Here is the command to do the same thing:
    vim myfile.json
    The interface is slightly different, but you can do similar things:
    editing json files in vim
    I have a beginner’s guide about Vim here (it’s for the Raspberry Pi, but it might help on any Linux computer).

So, nothing terribly challenging for now, you can create JSON files with the “touch” or any text editor that runs in a terminal.

Check this: Tired of Raspberry Pi OS? Level up with these top-rated systems.

Always Forgetting Linux Commands?
Grab This Cheat Sheet!
I've compiled the must-know commands in a simple PDF for quick reference.

Download now

Next, let’s see how to edit existing JSON files without messing up the formatting.

Editing a JSON File in the Linux Terminal

JSON files are generally fairly easy for humans to read, but writing them can be challenging. There is a full set of rules to keep in mind when you edit JSON files:

  • Data is in name-value pairs: You can’t have a value alone, ex: “name”: “Patrick”.
  • Each data type is formatted differently:
    • Strings between double quotes.
    • Numbers without them.
    • Object wrapped in curly braces.
    • Arrays in square brackets.
    • You can also have booleans (true/false) or ‘null’ as a value.
  • No trailing commas: the name pair in an object or array can’t have a comma after it.

And sadly, it was a bit of a shock for me while doing the research for this article: there are no great editors for JSON files when you don’t have a GUI.

Prefer reading without ads and popups?
Members get an ad-free version of every guide, plus exclusive project support.
Join the Community | Sign In

That’s it, there aren’t any good editor specifically built to edit JSON files in a Linux terminal, so we have to use the classic text editors you already know.

Basically, as JSON files are simply text files, you can use your favorite text editor for this (Nano, Vim, Emacs, etc.). Unlike YAML files, JSON files are not strict about white spaces (space, tabs, newlines, etc.), so it should be fine.

It’s not perfect, but for simple edits it should be fine. For larger modifications, check the end of the article, I still have a few tips for you.

Formatting JSON Files in the Terminal

Even if there isn’t a perfect text editor to work with JSON files from the command line, is there a way to make them easier to read and to work with? Yes, and I’ll show you how in this section.

Always Forgetting Linux Commands?
Grab This Cheat Sheet!
I've compiled the must-know commands in a simple PDF for quick reference.

Download now

The Problem

The main issue when working with JSON files in a Linux terminal is that it can be hard to read.

As I mentioned earlier, white spaces are not standardized, so computers (and some humans) tend to generate or write JSON files all in one continuous line.

They will often look like this:

unpretty json list

While it might be fine for short files like this example, it’s not the most readable.
When you open the file, I guess you would prefer to have something like this:

json file with pretty formatting

Interested? Well, stay with me, you just need to install a package that will do this for you quickly.

Solution: Installing jq

“jq” is a command-line processor for Linux. It’s not a text editor, but it can transform a JSON file in different ways. I’ll show you how to use it to prettify your JSON formatting, but it can also perform other tasks on JSON files (filtering, summarizing, inserting values, etc.).

Always Forgetting Linux Commands?
Grab This Cheat Sheet!
I've compiled the must-know commands in a simple PDF for quick reference.

Download now

For now, start by installing the jq package on your system. If you’re using a Debian-based distribution, you can get it with:
sudo apt install jq

install jq command line

It’s a tiny package, so it should be done pretty quickly.

Tip: Command lines can be a pain to memorize. I put the essential Linux commands on a printable cheat sheet so you don't have to keep googling them. You can grab the PDF here if you want to save some time.

Prettifying JSON Files

Now that jq is installed, you can start using it right away.

The main command you can use to display a JSON file properly follows this syntax :
jq . ugly.json

When you run it, it will output in your terminal directly (it doesn’t modify your original file). It adds spaces, tabs and new lines to make it easier for us to read (unlike “cat” or other commands).

jq formatting example
Always Forgetting Linux Commands?
Grab This Cheat Sheet!
I've compiled the must-know commands in a simple PDF for quick reference.

Download now

From there, if you want to save the changes to directly format a file like that, you can redirect the output to another file:
jq . ugly.json > pretty.json

Once done, you can use Nano or Vim to edit the new file, which should be easier than the original one now that it’s formatted properly.


🛠 This tutorial doesn't work anymore? Report the issue here, so that I can update it!

Stuck on this project? Ask me or other Pi users in the RaspberryTips Community. We help each other out and you'll get answers quick. Join and fix it together.

Need a Real Editor for JSON Files?

We have seen that there is no magical editor for JSON files in the Linux terminal, but it doesn’t mean that you have to do everything manually.

I guess that if you’re reading this article about editing JSON files via the Linux terminal, it’s because you work on a Linux server that doesn’t have a GUI. But often, these servers have some kind of remote access enabled (typically SSH).

If you can access the Linux server via SSH, it means you can copy the JSON file’s content and use a full editor on your computer. Or even better, you can use a free online tool.

While doing some research for this article, I stumbled upon this tool: JSON Editor Online. It looks like the screenshot below, and it was really what I was looking for, with features dedicated to the JSON format (adding objects, array, values, etc.).

json editor online

But obviously, if you can copy the JSON file to your computer, you can also use traditional text editors like VS Code, Sublime Text, or Geany. You can find a list of my favorite code editors here. All of them have features or plugins you can use to edit JSON files.

Whenever you're ready, here are other ways I can help you:

Master Linux Commands: Overwhelmed with Linux commands? This book is your essential guide to mastering the terminal. It includes practical tips, real-world examples, and a bonus cheat sheet to keep by your side.

The RaspberryTips Community: Need help with Linux or want to chat with people who actually get it? Join the RaspberryTips Community and get access to private forums, exclusive lessons, and direct support.

You can also find all my recommendations for tools and hardware on this page.

Similar Posts