Filtering
{"username":"jdoe","password":"sec\\ret$"} EOF $ jq . ./credentials.json { "username": "jdoe", "password": "sec\\ret$" } \```
jq
is the program to manipulate JSON in Bash. The most common use is to pull some information out of JSON files. It uses a filter language to manipulate the input. The simplest filter is the identity, .
(a single full stop). This is similar to mathematical identity: the input becomes the output. Let’s create a file containing some non–trivial JSON and apply the identity filter to it:
1
$ cat > ./credentials.json << 'EOF'
2
> {"username":"jdoe","password":"sec\\ret$"}
3
> EOF
4
$ jq . ./credentials.json
5
{
6
"username": "jdoe",
7
"password": "sec\\ret$"
8
}
The first command uses a quoted here document to save a string with special characters without having to escape them.
This page is a preview of The newline Guide to Bash Scripting