<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>TIL on CodeGoalie</title><link>https://codegoalie.com/categories/til/</link><description>Recent content in TIL on CodeGoalie</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Mon, 12 Feb 2024 16:09:11 -0500</lastBuildDate><atom:link href="https://codegoalie.com/categories/til/index.xml" rel="self" type="application/rss+xml"/><item><title>Format JSON in Nvim with jq</title><link>https://codegoalie.com/posts/format-json-nvim-jq/</link><pubDate>Mon, 12 Feb 2024 16:09:11 -0500</pubDate><guid>https://codegoalie.com/posts/format-json-nvim-jq/</guid><description>&lt;p>I often get large JSON files which contain no newlines or indentation. I&amp;rsquo;d like
to look at these reasonably in neovim, but it&amp;rsquo;s been cumbersome until today.&lt;/p>
&lt;p>TL;DR: You can replace the contents of the buffer with output formatted by jq
with one command: &lt;code>:%!jq .&lt;/code>&lt;/p>
&lt;p>Other techniques I&amp;rsquo;ve used, from worse to lest worse:&lt;/p>
&lt;ol>
&lt;li>Replacing every &lt;code>,&lt;/code> with &lt;code>,\r&lt;/code> and then using &lt;code>=&lt;/code> to auto-indent the lines.
This rarely works well.&lt;/li>
&lt;li>Using &lt;code>jq&lt;/code> externally. &lt;code>cat input.json | jq . &amp;gt; output.json&lt;/code>. This kind of
sucks because you now have 2 files&amp;hellip;&lt;/li>
&lt;/ol>
&lt;p>Now, you can open the file in neovim and type &lt;code>:%!jq .&lt;/code>. Magically, the buffer
will be nicely formatted.&lt;/p>
&lt;p>Roughly, this is entering a command, &lt;code>:&lt;/code>, on the whole buffer, &lt;code>%&lt;/code>, then
executing a shell command, &lt;code>!&lt;/code>, of &lt;code>jq .&lt;/code> the most basic &lt;code>jq&lt;/code> invocation.
Presumably, you could entry and &lt;code>jq&lt;/code> filter command to form some other JSON
instead of just formatting the given JSON, but I&amp;rsquo;ll leave that up to you to play
with.&lt;/p>
&lt;p>Happy formatting!!&lt;/p>
&lt;p>&amp;ndash; Chris&lt;/p></description></item></channel></rss>