Parsing a JSON payload without a top-level keyword

Zig’s JSON parser doesn’t parse to an ArrayList(T), but you can write a wrapper that does by implementing jsonParse. See my reply on a related post.

[]u8 is perfectly acceptable here. []const u8 would mean the array data is immutable (i.e. name is immutable), but since parseFromSlice and it’s sibling parseFromSliceLeaky allocate memory for the parsed result there aren’t any concerns about the constness of the data (there may be in your application, but from an API level there aren’t).


Anyways, I’m not sure why you’re getting an error in the first place. I suspect it’s some other issue related to your input or type definition, as the contrived example you provided works on my machine (tested with Zig versions 0.12.0, 0.13.0, 0.14.0, and 0.15.0-dev.621+a63f7875f):

const std = @import("std");

const Thing = struct {
    name: []u8,
};

const Things = []Thing;

pub fn main() !void {
    const str =
        \\[{"name": "thing1"}, {"name": "thing2"}]
    ;

    const things = try std.json.parseFromSlice(Things, std.heap.page_allocator, str, .{});

    for (things.value) |thing| {
        std.debug.print("{s}\n", .{thing.name});
    }
}

This prints

thing1
thing2

to my console.