<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: cheatsheetmaker</title>
    <description>The latest articles on DEV Community by cheatsheetmaker (@cheatsheetmaker).</description>
    <link>https://dev.to/cheatsheetmaker</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F649719%2F69b5f3a3-819a-45e6-b31e-b6dc1ec8b1c6.png</url>
      <title>DEV Community: cheatsheetmaker</title>
      <link>https://dev.to/cheatsheetmaker</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cheatsheetmaker"/>
    <language>en</language>
    <item>
      <title>JQ Cheat Sheet</title>
      <dc:creator>cheatsheetmaker</dc:creator>
      <pubDate>Mon, 28 Jun 2021 09:35:00 +0000</pubDate>
      <link>https://dev.to/cheatsheetmaker/jq-cheat-sheet-4ka1</link>
      <guid>https://dev.to/cheatsheetmaker/jq-cheat-sheet-4ka1</guid>
      <description>&lt;p&gt;JQ is a lightweight and flexible command-line JSON processor. This is a cheatsheet of commands and function that I’ve found useful for quick reference. You can  &lt;a href="https://cheatsheetmaker.com/jq-cheat-sheet"&gt;get full JQ Cheat Sheet Here&lt;/a&gt; .&lt;/p&gt;

&lt;h2&gt;
  
  
  Common usages
&lt;/h2&gt;

&lt;p&gt;Here are some common ways jq is utilized.&lt;/p&gt;

&lt;h3&gt;
  
  
  Piping from curl
&lt;/h3&gt;

&lt;p&gt;# get the Astronomy Picture of the Day (APOD)&lt;br&gt;
curl -s &lt;a href="https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY"&gt;https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY&lt;/a&gt; | jq&lt;br&gt;
(Note that piping to jq with no jq script will just format and colorize the JSON. Nice!)&lt;/p&gt;
&lt;h3&gt;
  
  
  From a JSON file
&lt;/h3&gt;

&lt;p&gt;Just pass the file path after the jq script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;jq '.name' package.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  In a chain of pipes
&lt;/h3&gt;

&lt;p&gt;You’ll probably want to use the -r (raw) command if you’re using it in a pipeline or saving the output to a variable. -r gets rid of formatting like quotes and spaces for single values. For objects, it outputs valid JSON so it will have quotes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# this downloads the latest APOD and saves to a file
url="https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY"
curl -o apod.png "$(curl -s $url | jq -r '.hdurl')"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Common selectors
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Get a named property
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo '{"id": 1, "name": "Cam"}' | jq '.id'``
# 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo '{"nested": {"a": {"b": 42}}}' | jq '.nested.a.b'
# 42
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Get an array element by index
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo '[0, 1, 1, 2, 3, 5, 8]' | jq '.[3]'
# 3

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Get an array element’s property
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo '[{"id": 1, "name": "Mario"}, {"id": 2, "name": "Luigi"}]' | jq '.[1].name'
# Luigi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Slice an array
&lt;/h3&gt;

&lt;p&gt;Slices an array on by index.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo '["a", "b", "c", "d"]' | jq '.[1:3]'
# ["b", "c"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Either the first or last index can be omitted to go from the begining or end.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo '["a", "b", "c", "d"]' | jq '.[1:]'
# ["b", "c", "d"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Creating a new object&lt;/p&gt;

&lt;p&gt;The syntax looks like this: { myPropertyName: .propertyFromJSON }&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo '{ "a": 1, "b": 2 }' | jq '{ a: .b, b: .a }'
# { "a": 2, "b": 1 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>jq</category>
      <category>cheatsheet</category>
      <category>json</category>
    </item>
  </channel>
</rss>
