{"id":9554,"date":"2023-07-30T15:44:47","date_gmt":"2023-07-30T13:44:47","guid":{"rendered":"https:\/\/via-internet.de\/blog\/?p=9554"},"modified":"2023-07-30T15:44:54","modified_gmt":"2023-07-30T13:44:54","slug":"jq-cheatsheet","status":"publish","type":"post","link":"https:\/\/via-internet.de\/blog\/2023\/07\/30\/jq-cheatsheet\/","title":{"rendered":"JQ &#8211; Cheatsheet"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/gist.github.com\/olih\/f7437fb6962fb3ee9fe95bda8d2c8fa4?permalink_comment_id=4170140#installing-jq\"><\/a>Installing jq<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/gist.github.com\/olih\/f7437fb6962fb3ee9fe95bda8d2c8fa4?permalink_comment_id=4170140#on-mac-os\"><\/a>On Mac OS<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">brew install jq<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/gist.github.com\/olih\/f7437fb6962fb3ee9fe95bda8d2c8fa4?permalink_comment_id=4170140#useful-arguments\"><\/a>Useful arguments<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When running jq, the following arguments may become handy:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Argument<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>--version<\/code><\/td><td>Output the jq version and exit with zero.<\/td><\/tr><tr><td><code>--sort-keys<\/code><\/td><td>Output the fields of each object with the keys in sorted order.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/gist.github.com\/olih\/f7437fb6962fb3ee9fe95bda8d2c8fa4?permalink_comment_id=4170140#basic-concepts\"><\/a>Basic concepts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The syntax for jq is pretty coherent:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Syntax<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td>,<\/td><td>Filters separated by a comma will produce multiple independent outputs<\/td><\/tr><tr><td>?<\/td><td>Will ignores error if the type is unexpected<\/td><\/tr><tr><td>[]<\/td><td>Array construction<\/td><\/tr><tr><td>{}<\/td><td>Object construction<\/td><\/tr><tr><td>+<\/td><td>Concatenate or Add<\/td><\/tr><tr><td>&#8211;<\/td><td>Difference of sets or Substract<\/td><\/tr><tr><td>length<\/td><td>Size of selected element<\/td><\/tr><tr><td>|<\/td><td>Pipes are used to chain commands in a similar fashion than bash<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/gist.github.com\/olih\/f7437fb6962fb3ee9fe95bda8d2c8fa4?permalink_comment_id=4170140#dealing-with-json-objects\"><\/a>Dealing with json objects<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Description<\/th><th>Command<\/th><\/tr><\/thead><tbody><tr><td>Display all keys<\/td><td><code>jq 'keys'<\/code><\/td><\/tr><tr><td>Adds + 1 to all items<\/td><td><code>jq 'map_values(.+1)'<\/code><\/td><\/tr><tr><td>Delete a key<\/td><td><code>jq 'del(.foo)'<\/code><\/td><\/tr><tr><td>Convert an object to array<\/td><td><code>to_entries &amp;#124; map([.key, .value])<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/gist.github.com\/olih\/f7437fb6962fb3ee9fe95bda8d2c8fa4?permalink_comment_id=4170140#dealing-with-fields\"><\/a>Dealing with fields<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Description<\/th><th>Command<\/th><\/tr><\/thead><tbody><tr><td>Concatenate two fields<\/td><td><code>fieldNew=.field1+' '+.field2<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/gist.github.com\/olih\/f7437fb6962fb3ee9fe95bda8d2c8fa4?permalink_comment_id=4170140#dealing-with-json-arrays\"><\/a>Dealing with json arrays<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/gist.github.com\/olih\/f7437fb6962fb3ee9fe95bda8d2c8fa4?permalink_comment_id=4170140#slicing-and-filtering\"><\/a>Slicing and Filtering<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Description<\/th><th>Command<\/th><\/tr><\/thead><tbody><tr><td>All<\/td><td><code>jq .[]<\/code><\/td><\/tr><tr><td>First<\/td><td><code>jq '.[0]'<\/code><\/td><\/tr><tr><td>Range<\/td><td><code>jq '.[2:4]'<\/code><\/td><\/tr><tr><td>First 3<\/td><td><code>jq '.[:3]'<\/code><\/td><\/tr><tr><td>Last 2<\/td><td><code>jq '.[-2:]'<\/code><\/td><\/tr><tr><td>Before Last<\/td><td><code>jq '.[-2]'<\/code><\/td><\/tr><tr><td>Select array of int by value<\/td><td><code>jq 'map(select(. &gt;= 2))'<\/code><\/td><\/tr><tr><td>Select array of objects by value<\/td><td>** jq &#8216;.[] | select(.id == &#8220;second&#8221;)&#8217;**<\/td><\/tr><tr><td>Select by type<\/td><td>** jq &#8216;.[] | numbers&#8217; ** with type been arrays, objects, iterables, booleans, numbers, normals, finites, strings, nulls, values, scalars<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/gist.github.com\/olih\/f7437fb6962fb3ee9fe95bda8d2c8fa4?permalink_comment_id=4170140#mapping-and-transforming\"><\/a>Mapping and Transforming<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Description<\/th><th>Command<\/th><\/tr><\/thead><tbody><tr><td>Add + 1 to all items<\/td><td><code>jq 'map(.+1)'<\/code><\/td><\/tr><tr><td>Delete 2 items<\/td><td><code>jq 'del(.[1, 2])'<\/code><\/td><\/tr><tr><td>Concatenate arrays<\/td><td><code>jq 'add'<\/code><\/td><\/tr><tr><td>Flatten an array<\/td><td><code>jq 'flatten'<\/code><\/td><\/tr><tr><td>Create a range of numbers<\/td><td><code>jq '[range(2;4)]'<\/code><\/td><\/tr><tr><td>Display the type of each item<\/td><td><code>jq 'map(type)'<\/code><\/td><\/tr><tr><td>Sort an array of basic type<\/td><td><code>jq 'sort'<\/code><\/td><\/tr><tr><td>Sort an array of objects<\/td><td><code>jq 'sort_by(.foo)'<\/code><\/td><\/tr><tr><td>Group by a key &#8211; opposite to flatten<\/td><td><code>jq 'group_by(.foo)'<\/code><\/td><\/tr><tr><td>Minimun value of an array<\/td><td><code>jq 'min'<\/code>&nbsp;.See also min, max, min_by(path_exp), max_by(path_exp)<\/td><\/tr><tr><td>Remove duplicates<\/td><td><code>jq 'unique'<\/code>&nbsp;or&nbsp;<code>jq 'unique_by(.foo)'<\/code>&nbsp;or&nbsp;<code>jq 'unique_by(length)'<\/code><\/td><\/tr><tr><td>Reverse an array<\/td><td><code>jq 'reverse'<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Installing jq On Mac OS Useful arguments When running jq, the following arguments may become handy: Argument Description &#8211;version Output the jq version and exit with zero. &#8211;sort-keys Output the fields of each object with the keys in sorted order. Basic concepts The syntax for jq is pretty coherent: Syntax Description , Filters separated by a comma will produce multiple independent outputs ? Will ignores error if the type is unexpected [] Array construction {} Object construction + Concatenate or Add &#8211; Difference of sets or Substract length Size of selected element | Pipes are used to chain commands in a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-9554","post","type-post","status-publish","format-standard","hentry","category-allgemein"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/9554","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/comments?post=9554"}],"version-history":[{"count":1,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/9554\/revisions"}],"predecessor-version":[{"id":9555,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/posts\/9554\/revisions\/9555"}],"wp:attachment":[{"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/media?parent=9554"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/categories?post=9554"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/via-internet.de\/blog\/wp-json\/wp\/v2\/tags?post=9554"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}