Открыть json файл linux

How to Parse JSON With Shell Scripting in Linux?

JSON parsing is the procedure of converting Javascript Object Notation (JSON) into a format that the programming languages can interpret. When the bash scripting is dealing with APIs, it needs JSON parsing.

To parse JSON in shell scripting, there is no native support, but the jq command is used for this.

This guide explains the parsing of JSON data with shell scripting on Linux.

  • What is JSON Data?
  • Does Shell Scripting Support JSON?
  • Parse JSON Data Using Shell Script on Linux
    • Using the jq Command
    • Parse JSON Data Using the curl Command
    • Parse JSON Data Using the grep Command

    What is JSON Data?

    JSON is a well-structured data format that is primarily utilized in modern APIs. It is supported and used by all major programming languages. Most data is transferred between your system and database using the JSON format.

    It is easy for humans to read and more efficient for machines to parse and share because of its simplicity.

    Does Shell Scripting Support JSON?

    No, the shell scripting does not directly support JSON, but a command-line utility is known as JSON Processer or jq, using which is installed using these commands:

    $ sudo apt install jq #For Ubuntu/Debian $ sudo dnf install jq #For Fedora $ sudo pacman -Sy jq #For Manjaro

    In the above image, the installation of jq is confirmed.

    How to Parse JSON Data Using Shell Script on Linux Using the jq Command?

    To parse JSON data using a shell script on Linux is explained in these steps:

    Step 1: Create a JSON File

    The JSON data file is created under the name “names.json” using the nano editor. The example code is provided below:

    The above JSON file contains the data of people including their name and an ID

    Step 2: Create a Shell Script to Parse JSON Data

    Here, a shell script named “myscript.sh” is created to parse the JSON data:

    #!/bin/sh NAME[0]="John" NAME[1]="Joe" NAME[2]="Tom" NAME[3]="Daisu" NAME[4]="Doe" echo "First Index: $" echo "Second Index: $"

    Here, the script uses the “jq” command, which is followed by a dot (.) to filter out the records from the file “names.json.

    This script first defines the JSON data in a variable named json and uses the jq command to extract the number of people and the names of all people. The -r option for the jq command is used to output the names as raw strings, without quotation marks. The script then prints out the results.

    Step 3: Execute the Script

    To execute the above-created script, ensure it has the “execute” permission. To grant them, use this command:

    Now, the script is executable, which can be done using this command:

    As the above image shows, “myscript.sh” is successfully executed, and the JSON data is parsed.

    How to Parse JSON Using the curl Command on Linux?

    The curl command can be used to make a HTTP request to the server that serves the JSON data, and using the jq command, the data can be parsed.

    Here is the explanation of the command before it is executed:

    • curl invokes the command
    • -s is an option of the curl command to suppress the error and progress
    • https://jsonplaceholder.typicode.com/todos/8 is the URL for the JSON data
    • The jq command is used to pretty-print the entire JSON data or object
    $ curl -s https://jsonplaceholder.typicode.com/todos/8 | jq '.'

    The above command displays the JSON data from the URL.

    How to Parse JSON Data Using the grep Command on Linux?

    The grep is primarily used for pattern matching within the given files and is not recommended to be used for JSON parsing. However, the JSON file can be filtered. Here is the explanation of the command used below to match the pattern:

    • grep invokes the command
    • -o is used to output only the matched pattern
    • “[^”]* searches all the data but ignores the double quotes “ “.
    • names.json is the input file name
    • grep -o ‘[^”]*$’ takes the output from the previous grep command and further extracts the values by removing everything up to the last double quote.

    Let’s execute this command and see the results:

    $ grep -o '"name": "[^"]*' names.json | grep -o '[^"]*$'

    Conclusion

    The bash scripting may not natively support parsing of JSON data (as of yet), but thanks to the “jq” command of Linux, it is possible. It can be combined with the curl command to make HTTP requests to the server that serves the JSON data. Python libraries can also be used for this purpose and are elaborated on in this detailed guide. Today, the whole procedure of parsing JSON with shell scripting is discussed.

    TUTORIALS ON LINUX, PROGRAMMING & TECHNOLOGY

    Источник

    Bash jq command

    JSON data are used for various purposes. But JSON data can’t be read easily from JSON file by using bash script like other normal files. jq tool is used to solve this problem. jq command works like sed and awk command, and it uses a domain specific language for working with JSON data. jq is not a built-in command. So, you have to install this command for using it. How you can install and apply jq command for reading or manipulating JSON data is shown in this tutorial.

    jq installation

    Run the following command to install jq on Ubuntu.

    Reading JSON data

    Suppose, you have declared a JSON variable named JsonData in the terminal and run jq command with that variable to print the content of that variable.

    Reading JSON data with –c option

    -c option uses with jq command to print each JSON object in each line. After running the following command, each object of JsonData variable will be printed.

    Reading a JSON file

    jq command can be used for reading JSON file also. Create a JSON file named Students.json with the following content to test the next commands of this tutorial.

    Students.json

    Run the following command to read Students.json file.

    Reading JSON file with ‘|’

    You can use ‘|’ symbol in the following way to read any JSON file.

    Reading single key values

    You can easily read any particular object from a JSON file by using jq command. In Students.json, there are four objects. These are roll, name, batch, and department. If you want to read the value of department key only from each record then run jq command in the following way.

    Reading multiple keys

    If you want to read two or more object values from JSON data then mention the object names by separating comma (,) in the jq command. The following command will retrieve the values of name and department keys.

    Remove key from JSON data

    jq command is used not only for reading JSON data but also to display data by removing the particular key. The following command will print all key values of Students.json file by excluding batch key. map and del function are used in jq command to do the task.

    Mapping Values

    Without deleting the key from JSON data, you can use map function with jq command for various purposes. Numeric values of JSON data can be increased or decreased by map function. Create a JSON file named Number.json with the following content to test the next commands.

    Run the following command to add 10 with each object value of Numbers,json.

    Run the following command to subtract 10 from each object value of Numbers,json.

    Searching values by index and length

    You can read objects from JSON file by specifying the particular index and length. Create a JSON file named colors.json with the following data.

    Run the following command to read two values starting from the third index of colors.json file.

    You can specify the length or starting index to read data from JSON file. In the following example, the number of data value is given only. In this case, the command will read four data from the first index of colors.json.

    You can specify the starting point only without any length value in jq command and the value can be positive or negative. If the starting point is positive then the index will count from the left side of the list and starting from zero. If the starting point is negative then the index will count from the right side of the list and starting from one. In the following example, the starting point is -3. So, the last three values from the data will display.

    When you will work with JSON data and want to parse or manipulate data according to your requirements then jq command will help you to make your task easier.

    About the author

    Fahmida Yesmin

    I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.

    Источник

    Открыть json файл linux

    jless is a command-line JSON viewer designed for reading, exploring, and searching through JSON data.

    jless will pretty print your JSON and apply syntax highlighting. Use it when exploring external APIs, or debugging request payloads.

    Expand and collapse Objects and Arrays to grasp the high- and low-level structure of a JSON document. jless has a large suite of vim-inspired commands that make exploring data a breeze.

    jless supports full text regular-expression based search. Quickly find the data you’re looking for in long String values, or jump between values for the same Object key.

    Installation

    jless currently supports macOS and Linux and can be installed using various package managers.

    OS Package Manager Command
    macOS HomeBrew brew install jless
    macOS MacPorts sudo port install jless
    Linux HomeBrew brew install jless
    Arch Linux pacman -S jless
    Void Linux sudo xbps-install jless
    NetBSD pkgin install jless
    FreeBSD pkg install jless

    If you have a Rust toolchain installed, you can also install directly from source using cargo:

    Binaries for various architectures are also available on GitHub.

    Check out the user guide to learn about the full functionality of jless.

    Источник

    Читайте также:  Suse linux enterprise server настройка сети
Оцените статью
Adblock
detector