![]() ![]() But keep in mind how handy it is when you need to create new JSON, or update an existing JSON document as well. It’s the first tool you should reach for when you need to parse JSON on the command line. In this age of JSON APIs and JSON configuration files, there are endless ways a fantastic command-line utility like jq can be used to make your life easier. ![]() The result of that change is piped into another usage of the update operator – this time to change the command. This uses jq’s powerful filtering capability to find the object in the environment array that has the name of “VERSION”, and then updates the value property. '(.definition.environment | select(.name = "VERSION") |. # These would likely be command-line arguments in a real script Picking up the release script example again, we’ll pipe multiple updates together to make all of the necessary changes in a single command: We’ll use it here to generate the request body: And it will properly quote the value when it generates the output. Jq has a -arg option that passes a value as a predefined variable, that can then be referenced in the output. ![]() But, after learning about jq’s ability to use arguments in its output, I haven’t had to worry about it again. I’m sure there’s some combination of single quotes, double quotes, and escaped quotes that will work. At least not for me – I run into issues with quoting every time I try. In some languages, interpolating a couple of variables into a small JSON blurb wouldn’t be that difficult. Instead, we’ll use environment variables to provide the values. Hard-coding the username and password in a script isn’t a great idea. It then returns a response object that includes an auth token: Let’s take, for example, an API that takes a username and password in a JSON object: That means I first need to authenticate with the API before making any additional requests. In most cases, the API I’m using requires an auth token. Once in a while, I’ll find myself needing to write a Bash script that uses curl to retrieve some data. We’ve written before about how you can use jq to parse JSON on the command line, but in this post I want to talk about using jq to create JSON data from scratch or make changes to existing data. Mostly I want to say that jq is agnostic as to localization, and explain where the host OS might do some that is visible to the jq user. Jq is an amazing little command line utility for working with JSON data. If we ever add something like a printf() (which has been requested, and with good reasons), we may have to say something about localization there as well (e.g., for number formatting). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |