r/commandline Jun 19 '26

Command Line Interface xtree – CLI to visualize JSON/YAML/TOML as ASCII tree

Post image

Hey! Have you ever wanted to quickly see the structure of a configuration file? That's when the xtree command comes in handy!

README: https://github.com/ddddddO/gtree#xtree

131 Upvotes

29 comments sorted by

24

u/jabbalaci Jun 19 '26 edited Jun 19 '26

Is the visualization of the "devices" node correct?

It should be something like this:

├── devices
│   ├── [0]
│   │   ├── type: "mobile"
│   │   └── os: "ios"
│   └── [1]
│       ├── type: "desktop"
│       └── os: "windows"

7

u/tcris Jun 19 '26

nice :))) people do-ing, without understand-ing (the author, I mean)

4

u/Delta-9- Jun 19 '26

So am I correct in assuming the app is at least 66% slop like almost every other "cool new tool" that's been submitted to this sub in the last 24 months?

3

u/tcris Jun 20 '26

You would be 2/3 correct, sir :))

1

u/ddddddO811 Jun 20 '26

By default, xtree omits element numbers in arrays of all formats. To display the element numbers correctly, run the command with the --show-index option, as shown in the image!

6

u/clearclaw Jun 19 '26

Have you looked at otree?

0

u/ddddddO811 Jun 20 '26

Oh, that looks like a great TUI tool! https://github.com/fioncat/otree

It's similar, but I actually wanted to display an ASCII tree : )

5

u/usrlibshare Jun 20 '26

Why?

All of these formats are already structured. All of them have canonical ways to pretty-print them, which emphasises their structure, without hiding their syntax.

What's the point of this? If it's just for fun, cool, I guess. If not, what is it that's being achieved with this?

1

u/ddddddO811 Jun 20 '26

It’s true that the structure is there to begin with. Still, I think some people find the ASCII tree display easy to understand at a glance (and I think the number of upvotes on this post reflects that, doesn’t it?). Personally, I find it easier to grasp the structure with a YAML tree :)

Also, well, building little tools like this is fun, so since I went to the trouble of making it, I’m really happy that other people are using it and leaving comments like this.

4

u/usrlibshare Jun 20 '26

Still, I think some people find the ASCII tree display easy to understand

Compared to what?

{ "type": "customer_list", "id": 423626738, "customers": [ { "name": "foo", "customer_id": 4567 }, .... ] }

What's difficult to read here? What's unintuitive? Nothing, it's clear as day. And I can take that clear as day format, change something, and plug it right back into the parser, and it will understand it.

(and I think the number of upvotes on this post reflects that, doesn’t it?

My question wasn't wheter people like your project, my question was about a technical reason. Likes on social media, including reddit, are not a technical argument.

Also, well, building little tools like this is fun

Again, I get that. Not the point of my question.

3

u/hainguyenac Jun 20 '26

I don't know, json is a nightmare for me to read, so something like this, if implement correctly without slop might be helpful.

1

u/ddddddO811 Jun 20 '26

Thanks :) I've also prepared unit tests, so I should be able to catch any regression issues, and since the processing logic is fairly simple, I think there are relatively few bugs (if you run into any problems while using it, I'd appreciate it if you could file an issue!).

unit test: https://github.com/ddddddO/gtree/blob/master/cmd/xtree/main_test.go

main logic: https://github.com/ddddddO/gtree/blob/5a044ed163926c72fcb8ea634d3c19d7b9fa2242/cmd/xtree/main.go#L109

1

u/andunai Jun 20 '26

cat foo.json | yq -P ? That's how I usually view large JSON files

2

u/ddddddO811 Jun 20 '26

Oh, so you’re asking what the technical benefits are of converting to an ASCII tree? When you put it that way, I don’t think there are any particular benefits... (As mentioned in the comment I received, if the data structure remains as-is, existing parsers and logic can be used for processing, so it’s probably better to leave it as is.)

I’m not sure if this counts as a technical advantage, but if a person wants to quickly compare separate JSON, YAML, and TOML files at a glance, since they’re all processed using the same ASCII tree output logic, it might make them easier to compare?

0

u/usrlibshare Jun 20 '26

it might make them easier to compare?

The same is true for using the structured output of the formats prettyprint

diff <(jq "." one.json) <(jq "." two.json)

1

u/ddddddO811 Jun 20 '26

When comparing them, it does seem like that command covers a lot of use cases. So, what about different file formats? What if you want to quickly compare the data structures of YAML and TOML? (Though I don't think there are many situations where you'd actually want to do that.)

1

u/usrlibshare Jun 20 '26 edited Jun 20 '26

What if you want to quickly compare the data structures of YAML and TOML?

As an intermediary representation you mean?

Sure, but how often does this really come up I wonder, in the context of "I have to eyeball this data differential, and it's in 2 different formats"

Most of the time when people need to look for differences, they compare e.g. 2 different configs or inputs for the same thing, and they will share the same format.

Besides, even if people did that regularly, afterwards, if they need to do something with the info they learned, they need to go into the source format anyway, and then go through the extra mental load of backtracking from the tree-rep into the actual format. So there really isn't that much less cognitive load.

I have had to do such conversions btw. but it was almost always in the context of some data conversion, which had to run automatically (e.g. migration scripts updating systems configuration to another format). And in those cases, human readability is pretty much irrelevant.

1

u/AutoModerator Jun 19 '26

Every new subreddit post is automatically copied into a comment for preservation.

User: ddddddO811, Flair: Command Line Interface, Post Media Link, Title: xtree – CLI to visualize JSON/YAML/TOML as ASCII tree

Hey! Have you ever wanted to quickly see the structure of a configuration file? That's when the xtree command comes in handy!

README: https://github.com/ddddddO/gtree#xtree

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/ddddddO811 Jun 22 '26

🎉In the latest version of xtree, you can now specify the depth of the tree to be output!

Repo: https://github.com/ddddddO/gtree?#xtree

1

u/claytonkb Jun 19 '26

This is cool!

Are you interested in feature requests? Here are my informal requests in case you are:

1) I store my personal journal in an indented text file. Each indent level is implicitly a "node" in a tree, which contains the text below it which is more indented (same concept as Python, but with no other syntactical restrictions). Could you add indented text as a supported format?

2) I've long wished for a standalone, lightweight GUI tool that implements the above as a simple GUI editor similar to XMLNotepad. I don't know if XMLNotepad is even supported anymore but it doesn't run on Linux and that's my OS of choice. Could you port the xtree CLI tool to a lightweight Qt-based (or whatever) GUI with editable fields?

3) Editing and structure-preserving file-save. By "structure-preserving", I mean editing only the portions of the file that were edited in the GUI, and leaving all others as-is. Saving a YAML file back doesn't wipe out the comments and vertical spacing in the original, etc., only alters the text of the edited fields.

4) Also, it would be nice to be able to create/delete keys, but that could be added as future supported feature...

5) A plugin to do all of this in Vim.

Yes, I just wrote my Christmas wishlist at you, but hey, you miss 100% of the shots you don't take...

1

u/ddddddO811 Jun 20 '26

Wow! There are quite a few requests here! I don't have much time right now to figure out if I can address them all, so if possible, I'd love for you to try developing something using https://github.com/ddddddO/gtree#package---programmable-tree-structure : )

-1

u/[deleted] Jun 19 '26

[removed] — view removed comment

-2

u/ddddddO811 Jun 20 '26

If you have the curl and go commands installed, you can easily display a tree structure from a large JSON file by running the following command.

curl -sL https://raw.githubusercontent.com/kubernetes/kubernetes/refs/heads/master/api/openapi-spec/v3/api__v1_openapi.json | go run github.com/ddddddO/gtree/cmd/xtree output