<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>yairlenga</title>
    <link rel="alternate" type="text/html" href="https://blogs.perl.org/users/yairlenga/" />
    <link rel="self" type="application/atom+xml" href="https://blogs.perl.org/users/yairlenga/atom.xml" />
    <id>tag:blogs.perl.org,2009-11-03:/users/yairlenga//4859</id>
    <updated>2026-06-30T15:30:25Z</updated>
    <subtitle>A blog about the Perl programming language</subtitle>
    <generator uri="http://www.sixapart.com/movabletype/">Movable Type Pro 4.38</generator>

<entry>
    <title>JSON::JSONFold - a CPAN module for compact, readable JSON formatting</title>
    <link rel="alternate" type="text/html" href="https://blogs.perl.org/users/yairlenga/2026/06/jsonjsonfold---a-cpan-module-for-compact-readable-json-formatting.html" />
    <id>tag:blogs.perl.org,2026:/users/yairlenga//4859.12074</id>

    <published>2026-06-30T15:17:54Z</published>
    <updated>2026-06-30T15:30:25Z</updated>

    <summary>I&apos;ve been working on a CPAN module called JSON::JSONFold, and I wrote an article describing the motivation and design. I&apos;d really appreciate feedback from other Perl developers. JSON serializers tend to give us two choices: compact JSON, which is efficient...</summary>
    <author>
        <name>Yair Lenga</name>
        <uri>https://github.com/yairlenga</uri>
    </author>
    
    
    <content type="html" xml:lang="en" xml:base="https://blogs.perl.org/users/yairlenga/">
        <![CDATA[<p>I've been working on a CPAN module called <a href="https://metacpan.org/pod/JSON::JSONFold"><strong>JSON::JSONFold</strong></a>, and I wrote an article describing the motivation and design. I'd really appreciate feedback from other Perl developers.</p>

<p>JSON serializers tend to give us two choices: compact JSON, which is efficient but a dense wall of text that's painful to read, or pretty-printed JSON, which is readable but often wastes a lot of vertical space (a small array of numbers can turn into ten lines).</p>

<p>I wanted something in between. JSONFold keeps the shape of pretty-printed JSON, but folds small, simple structures back onto a single line whenever that improves readability. It works on top of your existing serializer (<code>JSON</code>, <code>JSON::PP</code>, <code>JSON::XS</code>, etc.) - you keep using whatever you already have, and JSONFold just reformats the output.</p>

<h2>Example 1 - Coding</h2>

<pre><code>use JSON::JSONFold qw(encode_json);

my $data = {
    _id       =&gt; 123,
    locations =&gt; [
      { city =&gt; "Boston",    state =&gt; "MA", country =&gt; "USA" },
      { city =&gt; "Seattle",   state =&gt; "WA", country =&gt; "USA" },
      { city =&gt; "Montreal",  state =&gt; "QC", country =&gt; "Canada" },
    ],
    info      =&gt; {
      roles     =&gt; [ "foo", "bar", "baz" ],
    },
    name      =&gt; "Alice",
};

print encode_json($data) ;
</code></pre>

<p>Output</p>

<pre><code>{
  "_id": 123,
  "info": { "roles": [ "foo", "bar", "baz" ] },
  "locations": [
    { "city": "Boston",   "country": "USA",    "state": "MA" },
    { "city": "Seattle",  "country": "USA",    "state": "WA" },
    { "city": "Montreal", "country": "Canada", "state": "QC" }
  ],
  "name": "Alice"
}
</code></pre>

<h2>Example 2 - Packing</h2>

<p>Traditional pretty-printing:</p>

<pre><code>{
  "states": [
    "Alabama",
    "Alaska",
    "Arizona",
    ...
    "Wyoming"
  ]
}
</code></pre>

<p>JSONFold:</p>

<pre><code>{
  "states": [
    "Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado",
    "Connecticut", "Delaware", "Florida", "Georgia", ...
    "West_Virginia", "Wisconsin", "Wyoming"
  ]
}
</code></pre>

<p>Same data, just using the available line width more effectively.</p>

<h2>Example 3 - Grid Formatting</h2>

<p>When an array contains repeated structures, JSONFold can align values into columns:</p>

<p>Traditional pretty-printing:</p>

<pre><code>[
  {
    "orders": 18,
    "product": "Laptop",
    "sales": 1250
  },
  ...
  {
    "orders": 24,
    "product": "Mouse",
    "sales": 1422
  }
]
</code></pre>

<p>JSONFold:</p>

<pre><code>[
  { "orders": 18, "product": "Laptop",   "sales": 1250 },
  { "orders": 21, "product": "Monitor",  "sales": 1345 },
  { "orders": 17, "product": "Keyboard", "sales": 1198 },
  { "orders": 24, "product": "Mouse",    "sales": 1422 }
]
</code></pre>

<p>The module also supports:</p>

<ul>
<li>Folding small arrays and objects onto a single line.</li>
<li>Joining adjacent folded objects to further reduce vertical space.</li>
<li>Compatibility APIs similar to <code>JSON</code> and <code>JSON::PP</code>.</li>
</ul>

<p>I wrote a more detailed article covering the design, implementation, and full set of examples:</p>

<p><strong>Medium:</strong> <a href="https://medium.com/p/a619c9e7c3ec">https://medium.com/p/a619c9e7c3ec</a></p>

<p><strong>CPAN:</strong> <a href="https://metacpan.org/pod/JSON::JSONFold">https://metacpan.org/pod/JSON::JSONFold</a></p>

<p><strong>GitHub:</strong> <a href="https://github.com/yairlenga/jsonfold/tree/main/perl">https://github.com/yairlenga/jsonfold/tree/main/perl</a></p>

<p>I'd love to hear what the Perl community thinks. Has anyone else run into JSON pretty-printing pain in logs, configs, or debugging output? And are there formatting styles or options you'd want to see?</p>
]]>
        

    </content>
</entry>

</feed>
