# Parsing ZON files (NOT build.zig.zon) from Zig code

**URL:** <https://ziggit.dev/t/parsing-zon-files-not-build-zig-zon-from-zig-code/2513>\
**Category:** Help\
**Tags:** standard-library\
**Created:** [December 19, 2023, 11:20am UTC](https://ziggit.dev/t/parsing-zon-files-not-build-zig-zon-from-zig-code/2513 "2023-12-19T11:20:45Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Durobot](https://ziggit.dev/user_avatar/ziggit.dev/durobot/32/447_2.png) [@Durobot](https://ziggit.dev/u/Durobot)\
**Post date:** [December 19, 2023, 11:20am UTC](https://ziggit.dev/t/parsing-zon-files-not-build-zig-zon-from-zig-code/2513/1 "2023-12-19T11:20:45Z")

</div>

Could somebody please point me in the direction of ZON parsing functionality in stdlib (I hope it’s there, where else would it be)?

I have found myself in need of a ‘config’ file format, and while I have used JSON, YAML, TOML and my own homegrown file formats in the past for this purpose, I think ZON is the obvious choice in Zig programs.

Upd: these seem to be relevant:  
[https://github.com/ziglang/zig/blob/master/lib/std/zig/Parse.zig](https://github.com/ziglang/zig/blob/master/lib/std/zig/Parse.zig)  
[https://github.com/ziglang/zig/blob/master/lib/std/zig/Ast.zig](https://github.com/ziglang/zig/blob/master/lib/std/zig/Ast.zig)  
but for me they’re a ball of yarn I’d have to untangle. Not that I don’t want to learn (I do), but abstract syntax trees are not my primary focus right now, and so I would be grateful if I could just throw a ZON file at a function and get some form of parsed result in return.

---

<div class="post-metadata">

**Author:** ![Luke](https://ziggit.dev/letter_avatar_proxy/v4/letter/l/7ab992/32.png) [@Luke](https://ziggit.dev/u/Luke)\
**Post date:** [December 19, 2023, 1:12pm UTC](https://ziggit.dev/t/parsing-zon-files-not-build-zig-zon-from-zig-code/2513/2 "2023-12-19T13:12:12Z")

</div>

Maybe reading the [`loadManifest`](https://github.com/ziglang/zig/blob/master/src/Package/Fetch.zig#L513) function can help in `Fetch.zig`?

---

<div class="post-metadata">

**Author:** ![Durobot](https://ziggit.dev/user_avatar/ziggit.dev/durobot/32/447_2.png) [@Durobot](https://ziggit.dev/u/Durobot)\
**Post date:** [December 19, 2023, 1:18pm UTC](https://ziggit.dev/t/parsing-zon-files-not-build-zig-zon-from-zig-code/2513/3 "2023-12-19T13:18:56Z")

</div>

Thank you.  
I’m currently looking at `fn parse` in Ast.zig: [zig/lib/std/zig/Ast.zig at master · ziglang/zig · GitHub](https://github.com/ziglang/zig/blob/master/lib/std/zig/Ast.zig#L53) , at it uses `std.zig.Parse`, hoping I can copy from it.

Update: well, this does do something:

```zig
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    const alloc8r = gpa.allocator();

    const zon_txt =
        \\.{
        \\ .name = "libffmpeg",
        \\ .version = "5.1.2",
        \\}
        ;

    var ast = try std.zig.Ast.parse(alloc8r, zon_txt, .zon);
    defer ast.deinit(alloc8r);
    std.debug.print("{}\n", .{ ast });

```

```zig
$ zig run zon_parse.zig 
zig.Ast{ .source = { 46, 123, 10, 32, 32, 32, 32, 46, 110, 97, 109, 101, 32, 61, 32, 34, 108, 105, 98, 102, 102, 109, 112, 101, 103, 34, 44, 10, 32, 32, 32, 32, 46, 118, 101, 114, 115, 105, 111, 110, 32, 61, 32, 34, 53, 46, 49, 46, 50, 34, 44, 10, 125 }, .tokens = multi_array_list.MultiArrayList(zig.Ast.TokenList__struct_4036).Slice{ .ptrs = { u8@7f8bf672b050, u8@7f8bf672b000 }, .len = 14, .capacity = 20 }, .nodes = multi_array_list.MultiArrayList(zig.Ast.Node).Slice{ .ptrs = { u8@7f8bf672b0e0, u8@7f8bf672b080, u8@7f8bf672b0a0 }, .len = 4, .capacity = 8 }, .extra_data = { }, .mode = zig.Ast.Mode.zon, .errors = { } }

```

---

<div class="post-metadata">

**Author:** ![Luke](https://ziggit.dev/letter_avatar_proxy/v4/letter/l/7ab992/32.png) [@Luke](https://ziggit.dev/u/Luke)\
**Post date:** [December 19, 2023, 1:38pm UTC](https://ziggit.dev/t/parsing-zon-files-not-build-zig-zon-from-zig-code/2513/4 "2023-12-19T13:38:48Z")

</div>

You can have a look at what [Manifest](https://github.com/ziglang/zig/blob/master/src/Package/Manifest.zig#L74) does and you’re all set.

---

<div class="post-metadata">

**Author:** ![Durobot](https://ziggit.dev/user_avatar/ziggit.dev/durobot/32/447_2.png) [@Durobot](https://ziggit.dev/u/Durobot)\
**Post date:** [December 19, 2023, 2:55pm UTC](https://ziggit.dev/t/parsing-zon-files-not-build-zig-zon-from-zig-code/2513/5 "2023-12-19T14:55:09Z")

</div>

I have found this post - [Zig Parser – Mitchell Hashimoto](https://mitchellh.com/zig/parser)

BTW, it looks like [Parse struct](https://github.com/ziglang/zig/blob/master/src/Package/Manifest.zig#L188) is not public, meaning that ZON parsing functionality is for not for external use either 😢

---

<div class="post-metadata">

**Author:** ![Durobot](https://ziggit.dev/user_avatar/ziggit.dev/durobot/32/447_2.png) [@Durobot](https://ziggit.dev/u/Durobot)\
**Post date:** [December 20, 2023, 1:38pm UTC](https://ziggit.dev/t/parsing-zon-files-not-build-zig-zon-from-zig-code/2513/6 "2023-12-20T13:38:19Z")

</div>

Just wanted to share that I’m making some progress by following @figsoda’s [zon2nix](https://ziggit.dev/t/zon2nix-convert-the-dependencies-in-build-zig-zon-to-a-nix-expression/1422/2), more specifically, function `parse`: [zon2nix/src/parse.zig at main · nix-community/zon2nix · GitHub](https://github.com/nix-community/zon2nix/blob/main/src/parse.zig#L12)

---

<div class="post-metadata">

**Author:** ![Durobot](https://ziggit.dev/user_avatar/ziggit.dev/durobot/32/447_2.png) [@Durobot](https://ziggit.dev/u/Durobot)\
**Post date:** [December 21, 2023, 3:36pm UTC](https://ziggit.dev/t/parsing-zon-files-not-build-zig-zon-from-zig-code/2513/7 "2023-12-21T15:36:19Z")

</div>

BTW, does ZON support arrays?

Both `.uint_arr = { 1, 2, 3, 4 },` and `.uint_arr = .{ 1, 2, 3, 4 },` seem to be passing `std.zig.Ast.parse` with flying colors.

I wonder which of them (if any) is correct.  
With `.uint_arr = .{` I’m getting `zig.Ast.Node.Tag.array_init_dot`, and with `.uint_arr_2 = {` - `zig.Ast.Node.Tag.block_two`, so I guess `.{` is correct. Maybe.

Or is it something like `[_]{ 1, 2, 3, 4 }` ?

---

<div class="post-metadata">

**Author:** ![Durobot](https://ziggit.dev/user_avatar/ziggit.dev/durobot/32/447_2.png) [@Durobot](https://ziggit.dev/u/Durobot)\
**Post date:** [December 28, 2023, 7:36am UTC](https://ziggit.dev/t/parsing-zon-files-not-build-zig-zon-from-zig-code/2513/8 "2023-12-28T07:36:57Z")

</div>

Ok, never mind that last question about arrays (yes, they are supported).

I think I have achieved what I wanted, here’s the link - [ZON field value extractor (for the lack of better name) - zon\_get\_fields](https://ziggit.dev/t/zon-field-value-extractor-for-the-lack-of-better-name-zon-get-fields/2615)

---

<div class="post-metadata">

**Author:** ![Cloudef](https://ziggit.dev/user_avatar/ziggit.dev/cloudef/32/1370_2.png) [@Cloudef](https://ziggit.dev/u/Cloudef)\
**Post date:** [January 11, 2024, 9:38am UTC](https://ziggit.dev/t/parsing-zon-files-not-build-zig-zon-from-zig-code/2513/9 "2024-01-11T09:38:50Z")

</div>

I made zon2json tool for my zig2nix repo [zig2nix/tools/zon2json at master · Cloudef/zig2nix · GitHub](https://github.com/Cloudef/zig2nix/tree/master/tools/zon2json)

---

<div class="post-metadata">

**Author:** ![Cloudef](https://ziggit.dev/user_avatar/ziggit.dev/cloudef/32/1370_2.png) [@Cloudef](https://ziggit.dev/u/Cloudef)\
**Post date:** [January 13, 2024, 5:30am UTC](https://ziggit.dev/t/parsing-zon-files-not-build-zig-zon-from-zig-code/2513/10 "2024-01-13T05:30:03Z")

</div>

I split it into separate module [zig2nix/tools/zon2json/src at master · Cloudef/zig2nix · GitHub](https://github.com/Cloudef/zig2nix/tree/master/tools/zon2json/src)  
Not sure if zig package manager can access subpaths in git repo though.  
It’s mainly used to generate lockfiles for nix [zig2nix/tools/fixtures/2.zig.zon2json-lock at master · Cloudef/zig2nix · GitHub](https://github.com/Cloudef/zig2nix/blob/master/tools/fixtures/2.zig.zon2json-lock)

Though I may eventually replace this tool, reason here:

> <https://github.com/Cloudef/zig2nix/issues/1>
>
> While zon2json works right now for any \`build.zig.zon\` files I've thrown at it. …The ast walker itself isn't really complete. ~~Zon files itself are literally zig structs, so they may contain actual zig expressions I don't intend to maintain a zig interpreter so~~ consider instead making zon2json a program that depends on zig compiler that generates a program that does something like:
> \`\`\`zig
> const zon = \<insert .zon contents here\>;
> std.io.getStdout().writer().print("{s}", std.json.fmt(zon, .{}));
> \`\`\`
> 
> This would make sure zon files are always handled properly. cons might be speed, but zig should cache the outputs.
> 
> At some point it might become possible to import zon files as well: https://github.com/ziglang/zig/issues/14531
> 
> ref: https://github.com/ziglang/zig/issues/15552
