#Error parsing json data with json.parseFromSlice() - Bug or my fault?

1 messages · Page 1 of 1 (latest)

undone spindle
#

Hi all, I am new to zig so I am not sure if I am doing something wrong or if there is a bug in the std.json library.
Practically the deserialization fails until I comment a field which apparently is harmless.
I got the json data from this site: https://dummyjson.com/products
I have reproduced the issue in a github repo but I don't know if it is allowed to post here the link to it.

Here below are the main parts of the code (the issue is with the field brand: []const u8. If I comment it all goes well but it skips that field.
Any ideas?

`const json_data = @embedFile("data.json");
const Products = struct {
products: []struct {
id: usize,
title: []const u8,
description: []const u8,
category: []const u8,
price: f16,
discountPercentage: f16,
rating: f16,
stock: usize,
tags: [][]const u8,
brand: []const u8,
sku: []const u8,
.....
},
total: usize,
skip: usize,
limit: usize,
};

.......

// Deserialize JSON
const options = std.json.ParseOptions{
.ignore_unknown_fields = true,
};
const parsed = try json.parseFromSlice(Products, allocator, json_data, options);
defer parsed.deinit();
const products = parsed.value;
`

abstract solstice
#

if the field is optional, you might make it optional and provide a default. that way the std.json parse() method will set it to null when missing.

brand: ?[]const u8 = null,
#

shameless plug, i've made a tool which creates zig 'schema' files from json. might be worth giving it a try and see if it produces that same field type with default null given your data: https://travisstaloch.github.io/

#

yeah i checked and it looks like it does that for the brand field

obtuse hatch
#

BTW it's fine to link to a github repo here.