قالب وردپرس درنا توس
Home / Tips and Tricks / Working with JSON on the command line – CloudSavvy IT

Working with JSON on the command line – CloudSavvy IT



  JSON Data

Linux does not have a built-in way to properly handle JSON, yet many APIs will return JSON output. Here's how to parse and use JSON data in your shell scripts with a simple utility.

Parsing JSON with jq

jq is a utility that can handle everything you want to do with JSON. It is super easy to install; just download the binary file and move it to your folder on your PATH, usually / usr / local / bin / . It has no dependencies, so it is only one binary file to download. There are also downloads for macOS and Windows.

For starters, you can direct JSON there to print it nicely. Some APIs narrow their responses to save space, which leads to this horrible mess when you curl something in the console:

 curl in the console

Direct it to ] jq like so:

  curl https://api.example.com/ | jq 

And it is now readable, with syntax accent to boot:

 Readable text with syntax highlighting

If you command jq it will use it to select data from the JSON, just like how sed works on text. We use this JSONPlaceholder as an example API to try out commands. For the sake of simplicity, we omit the curl command and pipe to keep everything readable. You can also crawl to a file and crawl cat to jq if you don't want to see the output of curl every time you test a new command.

A Simple Period & # 39; . & # 39; represents the data taken to jq . If you just want to get a key out of the object, you can do the following:

  jq & # 39; .status & # 39; 

You can also nest this; & # 39 ;. data.geo.host & # 39; For example, would return a value further down the list. Optionally, you can add a question mark for error checking such as " .data.geo?. Host ", indicating that the property is optional.

Working with arrays is where jq ] Gets interesting. You can use square brackets to get a specific array element. For example, you can get the name of the first item with:

  jq & # 39 ;. [0] .name & # 39; 

You can segment arrays with [2:5] and use the same question mark syntax for optional properties. However, keep in mind that if an array is empty, it does not mean that it is an invalid property.

However, you will probably want to retrieve a specific item based on the data in it, not the array order. To do that, pass the entire array to the utility select :

  jq & # 39 ;. [] | select (.name | contains ("Leanne")) & # 39; 

This filters the array to include only items that passed the test; in this case, does the field name contain a specific name? This can be any boolean expression, even arithmetic operations such as select (.count> = 2) .

Reformatting JSON

jq can also construct JSON, which means you can use it to reformat JSON input. You can easily create objects by wrapping them in curly braces and passing jq statements as values. jq will execute the new object. For example:

  cat json | jq & # 39 ;. [0] | {name: .name, company: .company} & # 39; 

Executes nicely to the command line:

 jq executes the new object

Although this is useful for only the To delete information you want, you can also construct new objects using jq commands.

For a complete reference on all syntax of jq please refer to the online manual.

Alternatives to jq

If you don't want to use any other program, you can use some alternatives. The simplest method is to not do it in bash and instead use a scripting language like Python or JavaScript (with node) to do the actual processing. You would instead pipe the JSON command output to a file:

  curl https: //api/example.com/> json.txt 

and then load it into a variable in the script for use.

You can also use simple text selection tools; For example, if you only need a specific key from an answer, you can use grep next to a regular expression to select the value:

  grep "name" | sed & # 39; s / "name": " (. * )" /  1 / & # 39; 

This can (and probably will) very easily break with any changes to the JSON. Passing the user API returned the name of each user, as well as the username (which also corresponded to grep) and the name of the company (which had the exact same key). Unless you plan to take this into account, you should use something that is aware of the structure of the JSON, rather than treating it as text.


Source link