{"id":19217,"date":"2023-08-23T05:10:17","date_gmt":"2023-08-23T03:10:17","guid":{"rendered":"https:\/\/raspberrytips.com\/?p=19217"},"modified":"2023-09-14T06:20:51","modified_gmt":"2023-09-14T04:20:51","slug":"edit-json-files-linux-terminal","status":"publish","type":"post","link":"https:\/\/raspberrytips.com\/edit-json-files-linux-terminal\/","title":{"rendered":"How To Create and Edit JSON Files In Linux Terminal"},"content":{"rendered":"\n
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.<\/p>\n\n\n\n
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.<\/strong><\/p>\n\n\n\n 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.<\/p>\n\n\n\n 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.<\/p>\n\n\n\n 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: 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:<\/p>\n\n\n\n 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.<\/p>\n\n\n\n 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:<\/p>\n\n\n\n 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<\/a>.<\/p>\n\n\n\n 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.<\/p>\n\n\n\n Basically, as JSON files are simply text files, you can use your favorite text editor for this (Nano, Vim, Emacs, etc.).<\/strong> Unlike YML files, JSON files are not strict about white spaces (space, tabs, newlines, etc.), so it should be fine.<\/p>\n\n\n\n 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.<\/p>\n\n\n\n The main issue when working with JSON files in a Linux terminal is that it can be hard to read.<\/strong><\/p>\n\n\n\n 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.<\/p>\n\n\n\n They will often look like this:<\/p>\n\n\n\nCreating a JSON File in Linux Terminal<\/h2>\n\n\n\n
<\/strong>touch myfile.json<\/code><\/p>\n\n\n\n
\n
nano myfile.json
<\/code>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<\/a>).
<\/li>\n\n\n\n
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<\/a> here (it’s for the Raspberry Pi, but it might help on any Linux computer).<\/li>\n<\/ul>\n\n\n\nEditing a JSON File in the Linux Terminal<\/h2>\n\n\n\n
\n
\n
Formatting JSON Files in the Terminal<\/h2>\n\n\n\n
The problem<\/h3>\n\n\n\n