Tag: jq
-
the Bitwarden command-line tool
- https://bitwarden.com/help/article/cli/
- https://stedolan.github.io/jq/ – “bw” (the CLI utility) works well with jq
-
extracting fields from a Jira “issue” (like a very nice and useful description)? how to get the markup source of the issue description?
No, Jira does not let you read (“per se“) the markup source of an “issue” – that’s why I went this way in the first place 🙄
- http://Jochen.Hayek.name/wp/blog-en/2015/12/02/jira-rest-api/ – using Jira’s REST API: a “…/rest/api/2/issue/…” URL
- http://Jochen.Hayek.name/wp/blog-en/2015/12/22/jq-json-processor/ – how to use
jqfor json-tidying
jq . X.json > X.pretty.json
- http://Jochen.Hayek.name/wp/blog-en/2018/01/17/json-extract-structure/ – extract the structure tree of your JSON data using
jqand a nice command line
jq -c 'path(..)|[.[]|tostring]|join("/")|"."+.' X.pretty.json- the field we are interested in is
fields/description
jq ".fields.description" X.json > X.description.txt
- save the value of that field to a separate file!
- remove the leading and trailing double-quotes!
- replace all
\"with ordinary double-quotes! - remove
\r! - replace
\nwith ordinary new-lines!
jq ".fields.description" X.json | perl -pe 's/^"(.*)"$/$1/; s/\\"/"/g; s/\\r//g; s/\\n/\n/g' > X.description.txt
- …
- looks good, doesn’t it?
-
another nice JSON file: how to extract its structure using jq
$ jq -c 'path(..)|[.[]|tostring]|join("/")|"."+.'The idea is to use one of the output lines in order to access the respective field on your next
jqcommand line:$ jq ".fields.description" X.json
-
O’Reilly Media book: JSON at Work – Practical Data Integration for the Web
- http://shop.oreilly.com/product/0636920028482.do
- https://www.safaribooksonline.com/library/view/json-at-work/9781491982389/
- https://www.safaribooksonline.com/library/view/json-at-work/9781491982389/#toc
- https://www.safaribooksonline.com/library/view/json-at-work/9781491982389/ch05.html#json_schema
- https://www.safaribooksonline.com/library/view/json-at-work/9781491982389/ch06.html#json_search : …,
jq,jqPlay,jq-tutorial, … - https://www.safaribooksonline.com/library/view/json-at-work/9781491982389/ch07.html#json_transform
- https://www.safaribooksonline.com/library/view/json-at-work/9781491982389/ch09.html#json_and_mongodb
- https://www.safaribooksonline.com/library/view/json-at-work/9781491982389/ch10.html#json_messaging_with_kafka
More than just a simple replacement for XML when you make an AJAX call, JSON is becoming the backbone of any serious data interchange over the Internet. This practical book shows web architects and developers how to harness the energy and enthusiasm around JSON to build truly elegant, useful, and efficient applications. It’s complete with examples in JavaScript, jQuery, HTML5, Ruby, and Java.
-
Stephen Dolan’s “jq” is a lightweight and flexible command-line JSON processor
- https://stedolan.github.io/jq/
- https://stedolan.github.io/jq/tutorial/
- https://stedolan.github.io/jq/manual/
- https://github.com/stedolan/jq
- https://github.com/stedolan/jq/wiki
- https://github.com/stedolan/jq/wiki/Cookbook
- https://stackoverflow.com/questions/tagged/jq
- http://shop.oreilly.com/product/0636920032823.do – o’Reilly “Data Science at the Command Line” – has some examples making use of jq
- https://library.oreilly.com/book/0636920032823/data-science-at-the-command-line/84.xhtml?ref=toc#_jq – behind a paywall
- available as source (portable C) and as executables for various Intel-based platforms, i.e. some Linux distributions, Mac OS X, Windows incl. Cygwin
- https://cygwin.com/packages/x86_64/jq/
- https://www.safaribooksonline.com/library/view/json-at-work/9781491982389/ch06.html#json_search : …,
jq,jqPlay,jq-tutorial, …
“jq” (presumably) stands for “JSON query processor”.
jq helps writing shell scripts processing JSON responses from e.g. RESTful application APIs like Jenkins, Atlassian JIRA, Atlassian Confluence, … – code making use of powerful means like XPath (…) or jq is supposedly far more readable than Python / Perl / … scripts slurping the JSON and processing it (w/o jq resp. XPath) – but there are also Python resp. Perl bindings for jq.
Hope and fear
A couple of weeks ago I had written a shell script querying a Jenkins server’s REST API “à la XML”. Now it looks a little straighter to query the API “à la JSON” and employ jq. But the critical question is, whether the (industrial) customer (I wrote the Jenkins utility for) will like the dependency on a utility like jq. My Jenkins utility might have to serve for quite a couple of years – but who can predict the future and availability of jq?
Update 2016-02-23: Meanwhile I built a shell script with a couple of simple jq queries accessing Jenkins CI. Looks rather impressive to me.
If you do XPath queries from shell scripts, you always have the option to rewrite the shell script as Python or Perl script. But what about JSON queries (…) in Python or Perl?
- https://pypi.python.org/pypi/jq
- https://github.com/spiritloose/JQ — “Perl binding for jq”
By default, jq pretty-prints JSON output.
$ jq . < … > …
-
the JIRA REST API, how to authenticate, …
- https://en.wikipedia.org/wiki/Jira_(software)
- https://developer.atlassian.com/jiradev/jira-apis/jira-rest-apis/jira-rest-api-tutorials/jira-rest-api-example-basic-authentication
- https://developer.atlassian.com/jiradev/jira-apis/jira-rest-apis/jira-rest-api-tutorials/jira-rest-api-example-cookie-based-authentication
- https://metacpan.org/module/json_pp – a nice utility, that also does JSON pretty-printing; it comes with http://search.cpan.org/perldoc?JSON
There are certainly legion of reasons to use a REST API and also to use the JIRA REST API, I wanted to create a linear “diary” of JIRA actions.
This is our sample JIRA issue URL:
http://kelpie9:8081/browse/QA-31
This is its corresponding REST URL:
http://kelpie9:8081/rest/api/2/issue/QA-31
Find yourself a working sample JIRA issue URL use the corresponding REST URL in your browser, save the JSON returned to a file!
You usually want to read “pretty” / tidied JSON, so before you start reading JSON, find yourself a JSON-tidy utility:
Usually we want to retrieve JSON from JIRA through REST URLs via the curl utility.
CAVEAT: See my note on the cookie jar below!
This is the “simple example”, that the page referred to above (“Basic Authentication“) shows you:
$ curl -D- -u fred:fred -X GET
-H "Content-Type: application/json"
http://kelpie9:8081/rest/api/2/search?jql=assignee=fredIf your JIRA site requires you to use “Basic Authentication”, you have to encode username:password base64-wise, and this is how to do it:
$ echo -n fred:fred | base64So if you want to use “Basic Authentication” with these credentials, this is how … (using our sample REST URL):
$ curl -D- -X GET
-H "Authorization: Basic $(echo -n fred:fred | base64)"
-H "Content-Type: application/json"
"http://kelpie9:8081/rest/api/2/issue/QA-31"During my experiments I got locked out of the company’s Active Directory / SSO quite a few times — and I had to call the help desk in order to get my account reset. This is what JIRA tells you, once it decides you have to go through a CAPTCHA_CHALLENGE procedure, because you are behaving a little too suspicious:
HTTP/1.1 403 Forbidden
Server: Apache-Coyote/1.1
X-AREQUESTID: ...
X-Seraph-LoginReason: AUTHENTICATED_FAILED
Set-Cookie: JSESSIONID=...; Path=/; Secure; HttpOnly
WWW-Authenticate: OAuth realm="https%3A%2F%2Fjira.___.com"
X-Content-Type-Options: nosniff
X-Authentication-Denied-Reason: CAPTCHA_CHALLENGE; login-url=https://jira.___.com/login.jsp
Content-Type: text/html;charset=UTF-8
Content-Length: 6494
Date: Wed, 02 Dec 2015 11:59:15 GMT
But once you are beyond this, making use of the JIRA REST API works like a charm.
Update: Although I certainly had not failed (“basic”) authentication, JIRA got my Active Directory / SSO account locked again and again. My new strategy:
- 1st logon through “basic authentication” and store the cookie jar
- further authentications (during a script run) though the cookie stored before — yes, I will supply you with examples here in the near future
Wishlist:
- instead of shell+curl use perl+libcurl
- use the “epic link” to get the “epic link nice name” in order to describe the issue as “issue# + epic-link-nice-name + summary”
- extend the tool to also deal with Atlassian Confluence