Open json file in linux

Read a json file with bash

I kwould like to read the json file from http://freifunk.in-kiel.de/alfred.json in bash and separate it into files named by hostname of each element in that json string. How do I read json with bash?

3 Answers 3

How do I read json with bash?

You can use jq for that. First thing you have to do is extract the list of hostnames and save it to a bash array. Running a loop on that array you would then run again a query for each hostname to extract each element based on them and save the data through redirection with the filename based on them as well.

The easiest way to do this is with two instances of jq — one listing hostnames, and another (inside the loop) extracting individual entries.

This is, alas, a bit inefficient (since it means rereading the file from the top for each record to extract).

while read -r hostname; do [[ $hostname = */* ]] && continue # paranoia; see comments jq --arg hostname "$hostname" \ '.[] | select(.hostname == $hostname)' "out-$.json" done < <(jq -r '.[] | .hostname'  

(The out- prefix prevents alfred.json from being overwritten if it includes an entry for a host named alfred ).

Yes jq is a nice tool, but it seems it won't work with just one jq instance. So in the end I will use python, I think

@rubo77, indeed; personally, I'd probably have written this tool from python to begin with, rather than as a shell script calling something else.

I would use a subfolder to store the files: nodes='cat alfred.json'; files=logs/nodes; mkdir -p $files; $nodes|jq '.[].network.mac'|while read i; do $nodes |jq '.['$i']' > $files/$($nodes |jq '.['$i'].hostname'); done

@rubo77, the code you pasted there is somewhat broken (albeit most of those bugs being obscure/hard to hit, but better to be robust even against obscure inputs when you're working with untrusted data off the Internet). I used --arg in my sample for a reason -- otherwise, your $i can be treated as part of the jq query, rather than part of a value. Also, expansions need to be quoted to avoid string-splitting and glob expansion, and read unescapes backslash expansions unless the -r argument is passed.

@ruby77, . actually, since you're using random-off-the-Internet data, it'd also be worth being cautious about your data containing malicious "hostnames" like ../../../../../../etc/passwd ; see the according edit. 🙂

Источник

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.

Читайте также:  Стать владельцем файла linux

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.

Читайте также:  Astra 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

    Источник

    How do I open a JSON file in Linux?

    Vim is a file opener software that can be used to open the JSON file on Linux platform. GitHub Atom is a cross-platform tool to open JSON files….Below is a list of tools that can open a JSON file on the Windows platform:

    1. Notepad.
    2. Notepad++
    3. Microsoft Notepad.
    4. Microsoft WordPad.
    5. Mozilla Firefox.
    6. File Viewer Plus.
    7. Altova XMLSpy.

    What is JQ in Linux?

    jq is a lightweight and flexible command-line JSON processor. If you are a command line addict, you will like the official description. jq is like sed for JSON data – you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text.

    Is JQ installed by default?

    By default, the jq binary installed with the package is used. If you have special needs or want to use another binary in a different path you can set the environment variable JQ_PATH to override the default behaviour.

    Does Linux die?

    Linux isn’t dying anytime soon, programmers are the main consumers of Linux. It will never be as big as Windows but it will never die either. Linux on desktop never really worked because most computers don’t come with Linux preinstalled, and most people will never bother installing another OS.

    Why Linux is not popular as Windows?

    The main reason why Linux is not popular on the desktop is that it doesn’t have “the one” OS for the desktop as does Microsoft with its Windows and Apple with its macOS. If Linux had only one operating system, then the scenario would be totally different today. Linux kernel has some 27.8 million lines of code.

    What can Windows do that Linux can t?

    What Can Linux Do that Windows Can’t?

    • Linux will never harass you relentlessly to update.
    • Linux is feature-rich without the bloat.
    • Linux can run on almost any hardware.
    • Linux changed the world — for the better.
    • Linux operates on most supercomputers.
    • To be fair to Microsoft, Linux can’t do everything.

    Источник

Оцените статью
Adblock
detector