how to edit json files in linux terminal

How To Create and Edit JSON Files In Linux Terminal

JSON files are widely used by developers to save configurations and share data across servers and applications. They’re built to be easy for both machines and people to understand. But if you only have a Linux terminal to work with, it’s not that easy. 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.

Linux doesn’t have to be intimidating. With my e-book, Master Linux Commands, you’ll uncover the secrets of the terminal in a fun, step-by-step journey. From basics to scripts, get ready to level up your Linux skills. Oh, and did I mention the handy cheat sheet you get as a bonus?

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).

  • 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:

    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. But let’s see how to edit existing JSON files without messing up the formatting.

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

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 is no great editor for JSON files when you don’t have a GUI.

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

Join Our Community!

Connect, learn, and grow with other Raspberry Pi enthusiasts. Support RaspberryTips and enjoy an ad-free reading experience. Get exclusive monthly video tutorials and many other benefits.

Learn more

Basically, as JSON files are simply text files, you can use your favorite text editor for this (Nano, Vim, Emacs, etc.). Unlike YML 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

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 line.

They will often look like this:

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:

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

Interested? Well, stay with me, you just need to install one 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 files, but it can also do various tasks on JSON files (filtering, summarizing, inserting values, etc.).

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

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

If you’re new to the Linux command line, this article will give you the most important Linux commands to know, plus a free downloadable cheat sheet to keep handy.

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 display the file in your terminal directly (it doesn’t modify it), but it adds spaces, tabs and new lines to make it easier for us to read (unlike “cat” or other commands).

From there, if you want to save the changes to get a file directly formatted 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.

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

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 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 this, and it was really what I was looking for, with features dedicated to the JSON format (adding objects, array, values, etc.).

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.

Your Go-To Linux Command Reference!
Download your exclusive free PDF containing the most useful Linux commands to elevate your skills!
Download now

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

If you are looking for exclusive tutorials, I post a new course each month, available for premium members only. Join the community to get access to all of them right now!

Additional Resources

Overwhelmed with Linux commands?
My e-book, “Master Linux Commands”, is your essential guide to mastering the terminal. Get practical tips, real-world examples, and a bonus cheat sheet to keep by your side.
Grab your copy now.

VIP Community
If you just want to hang out with me and other Linux fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.

Need help building something with Python?
Python is a great language to get started with programming on any Linux computer.
Learn the essentials, step-by-step, without losing time understanding useless concepts.
Get the e-book now.

Similar Posts