If you want to read and output the text from a JSON file, you can do that in many ways. Let's take a look at some of them.
Option 1: Using cat command
# cat data.json
{
"city": "New York",
"forecast": [
{ "date": "2023-08-18", "temp": 78 },
{ "date": "2023-08-19", "temp": 82 },
{ "date": "2023-08-20", "temp": 75 },
{ "date": "2023-08-21", "temp": 70 },
{ "date": "2023-08-22", "temp": 73 }
]
}
Option 2: Using head command
If you want to read and display only x number of files from the top of the JSON file.
# head -n 5 data.json
{
"city": "New York",
"forecast": [
{ "date": "2023-08-18", "temp": 78 },
{ "date": "2023-08-19", "temp": 82 },
Option 3: Using tail command
If you want to read and display only x number of files from the bottom of the JSON file.
# tail -n 5 data.json
{ "date": "2023-08-20", "temp": 75 },
{ "date": "2023-08-21", "temp": 70 },
{ "date": "2023-08-22", "temp": 73 }
]
}

If you want to read and parse or manipulate a JSON file, then you should make use of the jq binary.
Make sure to install jq package
apt install jq
Example: Parsing and Manipulating JSON file
-
Display the name of the city from the JSON file:
# jq '.city' data.json
"New York"
-
Display the temp for each day:
# jq '.forecast[].temp' data.json
78
82
75
70
73
-
Calculate average temp from JSON:
# jq '[.forecast[].temp] | add / length' data.json
75.6

-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Bash,
- Bash Hello World! Script Tutorial
- How to run bash command in background
- How to create new user account in Windows bash
- How to Compare Strings in Bash
- Bash: Allow Command to Fail without exiting Script
- Fix bash: script.sh: Permission denied Error
- Bash getopts Command Example
- How to fix bash ping command not found error
- How to add NewLine Character in Bash Script String
- How to Echo Bash Command to a File
- Bash Command to Find String in a File
- How to open new Terminal using Bash Command
- Bash How to Save Output of a Command to a Variable
- Download a SSL Certificate from a URL in Terminal
- Bash Command to Check IP Address
- Know Bash shell version command
- Bash Command To Check If File Exists
- How to run a Command in Bash Script
- How to Compare two Files in Bash Shell
- How to check if a variable is set in Bash Script or Not
- Convert String from uppercase to lowercase in Bash
- How to see Created Accessed Modified and Changed dates of a file using bash terminal command
- Bash Command To Get Current Time
- Bash command to List Files
- Fix: bash: syntax error near unexpected token
More Posts:
- How to fix java.net.NoRouteToHostException in Android Studio - Android-Studio
- Fix: Xbox Error Code: 0x80190190 - Microsoft
- How to install pip on macOS using terminal command [Python] - Python
- How to Setup maven on Mac (macOS) - Mac-OS-X
- Tool: Convert Cron Expression To Plain English Text (Supports Quartz) - Tools
- How to pass value to another Power Apps screen - PowerApps
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException - Java
- How to delete a Python Virtual Environment - Python