246 lines
7.7 KiB
HTML
246 lines
7.7 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; font-src 'self' data:; img-src 'self' data:; connect-src 'self'; base-uri 'self'; form-action 'self' https://defcon.social https://bsky.app;">
|
|
<meta http-equiv="X-Content-Type-Options" content="nosniff">
|
|
<link rel="stylesheet" href="../assets/css/style.css">
|
|
<link rel="icon" type="image/x-icon" href="../favicon.ico">
|
|
<script>
|
|
// Apply theme immediately to prevent flash
|
|
(function() {
|
|
const theme = localStorage.getItem('theme') ||
|
|
(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
|
|
document.documentElement.setAttribute('data-theme', theme);
|
|
})();
|
|
</script>
|
|
<title>jq Cheatsheet - Cheatsheets - Launch Pad</title>
|
|
</head>
|
|
<body>
|
|
<button class="theme-toggle" id="themeToggle" aria-label="Toggle dark mode">
|
|
<svg class="theme-icon theme-icon-moon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>
|
|
<svg class="theme-icon theme-icon-sun" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display: none;"><circle cx="12" cy="12" r="5"></circle><line x1="12" y1="1" x2="12" y2="3"></line><line x1="12" y1="21" x2="12" y2="23"></line><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line><line x1="1" y1="12" x2="3" y2="12"></line><line x1="21" y1="12" x2="23" y2="12"></line><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line></svg>
|
|
</button>
|
|
<br/><br/>
|
|
<div class="name">
|
|
__ _______________________ _________._________________________
|
|
\_ _____/ \______ \ / _ \ / _____/ / _____/ | | \_ _____/
|
|
| __) | _/ / /_\ \ / \ ___ / \ ___ | | | __)_
|
|
| \ | | \ / | \ \ \_\ \ \ \_\ \ | |___ | \
|
|
\___ / |____|_ / \____|__ / \______ / \______ / |_______ \ /_______ /
|
|
\/ \/ \/ \/ \/ \/ \/
|
|
</div>
|
|
<div class="blog-page-header">
|
|
<div class="blog-header-content">
|
|
<a href="/cheatsheets" class="back-link" title="Back to Cheatsheets">
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="42" height="42" viewBox="0 0 24 24" class="home-icon"><path fill="currentColor" d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/></svg>
|
|
</a>
|
|
<h1 class="blog-page-title">jq Cheatsheet</h1>
|
|
</div>
|
|
</div>
|
|
<div class="blog-post-container">
|
|
<div class="blog-posts-container" style="max-width: 900px; margin: 0 auto;">
|
|
<div class="blog-post">
|
|
<div class="blog-post-content">
|
|
<p><a href="index.html">← Back to cheatsheets</a></p>
|
|
<p><a href="../index.html">← Home</a></p>
|
|
<hr>
|
|
<p>jq is a lightweight and flexible command-line JSON processor. Parse, filter, transform, and manipulate JSON data with ease. Essential for API responses, configuration files, and data processing pipelines.</p>
|
|
<hr>
|
|
<h2>Basic Usage</h2>
|
|
<ul>
|
|
<li>jq '.' file.json - Pretty print JSON</li>
|
|
</ul>
|
|
<ul>
|
|
<li>jq '.key' file.json - Extract value by key</li>
|
|
</ul>
|
|
<ul>
|
|
<li>echo '{"key":"value"}' | jq '.' - Pipe JSON</li>
|
|
</ul>
|
|
<ul>
|
|
<li>jq -r '.key' file.json - Raw output (no quotes)</li>
|
|
</ul>
|
|
<hr>
|
|
<h2>Filtering</h2>
|
|
<ul>
|
|
<li>jq '.key' - Get value</li>
|
|
</ul>
|
|
<ul>
|
|
<li>jq '.key1.key2' - Nested access</li>
|
|
</ul>
|
|
<ul>
|
|
<li>jq '.[]' - Array iteration</li>
|
|
</ul>
|
|
<ul>
|
|
<li>jq '.[0]' - First element</li>
|
|
</ul>
|
|
<ul>
|
|
<li>jq '.[-1]' - Last element</li>
|
|
</ul>
|
|
<ul>
|
|
<li>jq '.[:3]' - First 3 elements</li>
|
|
</ul>
|
|
<ul>
|
|
<li>jq '.[] | select(.key == "value")' - Filter objects</li>
|
|
</ul>
|
|
<ul>
|
|
<li>jq '.[] | select(.key > 10)' - Numeric comparison</li>
|
|
</ul>
|
|
<hr>
|
|
<h2>Object Operations</h2>
|
|
<ul>
|
|
<li>jq '.key1, .key2' - Multiple keys</li>
|
|
</ul>
|
|
<ul>
|
|
<li>jq '{newkey: .oldkey}' - Rename key</li>
|
|
</ul>
|
|
<ul>
|
|
<li>jq '. + {newkey: "value"}' - Add key</li>
|
|
</ul>
|
|
<ul>
|
|
<li>jq 'del(.key)' - Delete key</li>
|
|
</ul>
|
|
<ul>
|
|
<li>jq 'keys' - List all keys</li>
|
|
</ul>
|
|
<ul>
|
|
<li>jq 'has("key")' - Check if key exists</li>
|
|
</ul>
|
|
<hr>
|
|
<h2>Array Operations</h2>
|
|
<ul>
|
|
<li>jq '.array[]' - Iterate array</li>
|
|
</ul>
|
|
<ul>
|
|
<li>jq '.array | length' - Array length</li>
|
|
</ul>
|
|
<ul>
|
|
<li>jq '.array | .[0:3]' - Slice array</li>
|
|
</ul>
|
|
<ul>
|
|
<li>jq '.array + ["new"]' - Append element</li>
|
|
</ul>
|
|
<ul>
|
|
<li>jq '.array | map(.key)' - Extract key from objects</li>
|
|
</ul>
|
|
<ul>
|
|
<li>jq '.array | sort' - Sort array</li>
|
|
</ul>
|
|
<ul>
|
|
<li>jq '.array | unique' - Remove duplicates</li>
|
|
</ul>
|
|
<ul>
|
|
<li>jq '.array | reverse' - Reverse array</li>
|
|
</ul>
|
|
<hr>
|
|
<h2>String Operations</h2>
|
|
<ul>
|
|
<li>jq '.key | length' - String length</li>
|
|
</ul>
|
|
<ul>
|
|
<li>jq '.key | split(",")' - Split string</li>
|
|
</ul>
|
|
<ul>
|
|
<li>jq '.key | startswith("prefix")' - Check prefix</li>
|
|
</ul>
|
|
<ul>
|
|
<li>jq '.key | endswith("suffix")' - Check suffix</li>
|
|
</ul>
|
|
<ul>
|
|
<li>jq '.key | contains("substring")' - Check substring</li>
|
|
</ul>
|
|
<ul>
|
|
<li>jq '.key | upper' - Uppercase</li>
|
|
</ul>
|
|
<ul>
|
|
<li>jq '.key | lower' - Lowercase</li>
|
|
</ul>
|
|
<hr>
|
|
<h2>Output Options</h2>
|
|
<ul>
|
|
<li>-r / --raw-output - Raw string output</li>
|
|
</ul>
|
|
<ul>
|
|
<li>-c / --compact-output - Compact JSON</li>
|
|
</ul>
|
|
<ul>
|
|
<li>-s / --slurp - Read entire input as array</li>
|
|
</ul>
|
|
<ul>
|
|
<li>-n / --null-input - Null input</li>
|
|
</ul>
|
|
<ul>
|
|
<li>--tab - Use tabs for indentation</li>
|
|
</ul>
|
|
<ul>
|
|
<li>--indent N - Indentation level</li>
|
|
</ul>
|
|
<hr>
|
|
<h2>Common Examples</h2>
|
|
<h3>Pretty Print</h3>
|
|
<pre><code>cat data.json | jq '.'</code></pre>
|
|
<p>Format JSON nicely.</p>
|
|
<h3>Extract Field</h3>
|
|
<pre><code>jq '.name' users.json</code></pre>
|
|
<p>Get name field from objects.</p>
|
|
<h3>Filter Array</h3>
|
|
<pre><code>jq '.[] | select(.status == "active")' users.json</code></pre>
|
|
<p>Filter objects by condition.</p>
|
|
<h3>Extract Multiple Fields</h3>
|
|
<pre><code>jq '{name: .name, id: .id}' users.json</code></pre>
|
|
<p>Create new object with selected fields.</p>
|
|
<h3>Array to CSV</h3>
|
|
<pre><code>jq -r '.[] | [.name, .email] | @csv' users.json</code></pre>
|
|
<p>Convert JSON array to CSV.</p>
|
|
<h3>Count Elements</h3>
|
|
<pre><code>jq '.items | length' data.json</code></pre>
|
|
<p>Count items in array.</p>
|
|
<h3>Sum Values</h3>
|
|
<pre><code>jq '[.[] | .price] | add' items.json</code></pre>
|
|
<p>Sum numeric values.</p>
|
|
<h3>Group By</h3>
|
|
<pre><code>jq 'group_by(.category) | map({category: .[0].category, count: length})' items.json</code></pre>
|
|
<p>Group objects by field.</p>
|
|
<h3>Flatten Nested</h3>
|
|
<pre><code>jq '.users[].name' data.json</code></pre>
|
|
<p>Extract from nested arrays.</p>
|
|
<h3>Merge Objects</h3>
|
|
<pre><code>jq '. + {timestamp: now}' data.json</code></pre>
|
|
<p>Add field to object.</p>
|
|
<hr>
|
|
<h2>Tips</h2>
|
|
<ul>
|
|
<li>Use -r for raw string output in scripts</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Use .[] to iterate arrays and objects</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Use select() for filtering</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Use map() to transform arrays</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Combine filters with | (pipe)</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Use @csv, @tsv for formatted output</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Essential for API response processing</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Great for configuration file manipulation</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<script async type="text/javascript" src="../blog/analytics.js"></script>
|
|
<script src="../theme.js"></script>
|
|
</body>
|
|
</html>
|
|
|