# Json Beautifier

**URL:** <https://racket.discourse.group/t/json-beautifier/1541>\
**Category:** Questions & Answers\
**Created:** [December 7, 2022, 2:11am UTC](https://racket.discourse.group/t/json-beautifier/1541 "2022-12-07T02:11:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@greg](https://racket.discourse.group/u/greg)\
**Post date:** [December 7, 2022, 2:11am UTC](https://racket.discourse.group/t/json-beautifier/1541/1 "2022-12-07T02:11:06Z")

</div>

Is there a way to "beautify" or format a json string in racket?  
E.g. from

```scheme
{ "name":"She-Ra","title":"The Princess of Power"}

```

to

```scheme
{
  "name": "She-Ra",
  "title": "The Princess of Power"
}

```

?

---

<div class="post-metadata">

**Author:** ![LiberalArtist](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/liberalartist/32/151_2.png) [@LiberalArtist](https://racket.discourse.group/u/LiberalArtist)\
**Post date:** [December 7, 2022, 2:27am UTC](https://racket.discourse.group/t/json-beautifier/1541/2 "2022-12-07T02:27:08Z")

</div>

Not yet, but there's an open PR!

> <https://github.com/racket/racket/pull/4300>
>
> Add \`#:indent\` to \`write-json\`. (See also https://github.com/racket/racket/pull/…4296).
> \`\`\`\`racket
> Welcome to Racket v8.5 \[cs\].
> \> (require json)
> \> (for (\[indent (in-list '(#f 0 4 #\\tab))\])
> (newline)
> (write-json #hasheq(\[waffle . (1 2 3)\] \[와플 . (1 2 3)\])
> #:indent indent)
> (newline))
> 
> {"waffle":\[1,2,3\],"와플":\[1,2,3\]}
> 
> {
> "waffle": \[1, 2, 3\],
> "와플": \[1, 2, 3\]
> }
> 
> {
> "waffle": \[1, 2, 3\],
> "와플": \[1, 2, 3\]
> }
> 
> {
> "waffle": \[1, 2, 3\],
> "와플": \[1, 2, 3\]
> }
> \`\`\`\`

---

<div class="post-metadata">

**Author:** ![EmEf](https://avatars.discourse-cdn.com/v4/letter/e/53a042/32.png) [@EmEf](https://racket.discourse.group/u/EmEf)\
**Post date:** [December 7, 2022, 2:42am UTC](https://racket.discourse.group/t/json-beautifier/1541/3 "2022-12-07T02:42:53Z")

</div>

My SwDev package (organically grown, undocument at this point) at [https://github.com/mfelleisen/SwDev](https://github.com/mfelleisen/SwDev) contains a module for pretty printing JSON:

[https://github.com/mfelleisen/SwDev/blob/master/Lib/json-pretty.rkt](https://github.com/mfelleisen/SwDev/blob/master/Lib/json-pretty.rkt)

It’s a WIP, started by Tony Garnock-Jones.

---

<div class="post-metadata">

**Author:** ![sorawee](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@sorawee](https://racket.discourse.group/u/sorawee)\
**Post date:** [December 7, 2022, 2:53am UTC](https://racket.discourse.group/t/json-beautifier/1541/4 "2022-12-07T02:53:25Z")

</div>

How much do you want the output to look "beautiful"?

Most JSON beautification uses a pretty printer to print the output in a way that doesn't exceed the width limit and looks nice. The proposed changes to `write-json` in the PR that @LiberalArtist referred to, on the other hand, employs some ad-hoc heuristics to make it look nicer than printing everything in one line, but the output is almost certainly going to be inferior to the pretty printing approach (though its advantage is that it will be faster to print).

I'm not aware of a package in Racket that does JSON beautification via pretty printing. However, there are packages that provide pretty printing functionality. [https://docs.racket-lang.org/pprint/](https://docs.racket-lang.org/pprint/), for instance, would be suitable for pretty printing JSON.

---

<div class="post-metadata">

**Author:** ![LiberalArtist](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/liberalartist/32/151_2.png) [@LiberalArtist](https://racket.discourse.group/u/LiberalArtist)\
**Post date:** [December 7, 2022, 3:31am UTC](https://racket.discourse.group/t/json-beautifier/1541/5 "2022-12-07T03:31:05Z")

</div>

> [@sorawee](#):
>
> Most JSON beautification uses a pretty printer to print the output in a way that doesn't exceed the width limit and looks nice. The proposed changes to `write-json` in the PR that @LiberalArtist referred to, on the other hand, employs some ad-hoc heuristics to make it look nicer than printing everything in one line, but the output is almost certainly going to be inferior to the pretty printing approach (though its advantage is that it will be faster to print).

I don't think I've seen a standard-library function that does JSON "pretty printing" in the sense you mention of being sensitive to the target width. The functions I've surveyed don't use a heuristic, either: they uniformly start a new line with some indentation for each array element or object key–value pair. I see now that [https://github.com/racket/racket/pull/4300](https://github.com/racket/racket/pull/4300) does currently use an ad-hoc heuristic: I've just commented there to say that I don't think it should.

> [@sorawee](#):
>
> However, there are packages that provide pretty printing functionality. [PPrint: A Universal Pretty-Printer](https://docs.racket-lang.org/pprint/), for instance, would be suitable for pretty printing JSON.

The [`racket/pretty`](https://docs.racket-lang.org/reference/pretty-print.html) library is another option for building that sort of thing.

---

<div class="post-metadata">

**Author:** ![greg](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@greg](https://racket.discourse.group/u/greg)\
**Post date:** [December 7, 2022, 12:15pm UTC](https://racket.discourse.group/t/json-beautifier/1541/6 "2022-12-07T12:15:36Z")

</div>

Thank you, I will try "racket/pretty" until the mr is merged

---

<div class="post-metadata">

**Author:** ![jesse](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jesse/32/446_2.png) [@jesse](https://racket.discourse.group/u/jesse)\
**Post date:** [December 10, 2022, 6:58am UTC](https://racket.discourse.group/t/json-beautifier/1541/7 "2022-12-10T06:58:08Z")

</div>

In the [argo JSON package](https://docs.racket-lang.org/argo/index.html), there's a pretty-print function (there's even a raco command!), but it's just a wrapper around `racket/pretty`, nothing fancy like what @sorawee has in mind. Looking forward to seeing that PR merged in!

---

<div class="post-metadata">

**Author:** ![LiberalArtist](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/liberalartist/32/151_2.png) [@LiberalArtist](https://racket.discourse.group/u/LiberalArtist)\
**Post date:** [December 12, 2022, 4:51am UTC](https://racket.discourse.group/t/json-beautifier/1541/8 "2022-12-12T04:51:51Z")

</div>

Since the JSON library is a single file, you could also grab the version from the PR for now and change `(require "json.rkt")` to `(require json)` once it's merged.
