I’m calling a 3rd party http endpoint that returns a json payload like this:
[{"name": "thing1"}, {"name": "thing2"}]
It’s obviously way more complicated than that, but you get the idea. The top level object is a list, without a corresponding keyword. Whether or not that’s “proper” JSON, it’s not my service. I’m just trying to use it.
Anyway, I don’t know how to create a struct to pass to parseFromSlice, because it doesn’t have a top-level key to put into the struct.
A top-level array is valid JSON, and if I remember correctly, Zig’s json package does parse it, though I was not specifically using parseFromSlice, which may have some additional restrictions.
A list on its own is valid JSON, and using a slice as the output type is correct, you don’t need to wrap this in a struct.
UnexpectedToken usually means that the type of the field doesn’t match what it found in the JSON. For example, a string instead of a boolean value (eg, “true”), or null for a field that’s not defined as an optional
This, in particular, led me down a path to troubleshooting my struct vs the actual JSON and seeing that I was truncating the payload, so it never actually was valid.
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):