Skip to main content
Version: Zig 0.12.0

JSON

Let's parse a JSON string into a struct type, using the streaming parser.

const Place = struct { lat: f32, long: f32 };

test "json parse" {
const parsed = try std.json.parseFromSlice(
Place,
test_allocator,
\\{ "lat": 40.684540, "long": -74.401422 }
,
.{},
);
defer parsed.deinit();

const place = parsed.value;

try expect(place.lat == 40.684540);
try expect(place.long == -74.401422);
}

And using stringify to turn arbitrary data into a string.

test "json stringify" {
const x = Place{
.lat = 51.997664,
.long = -0.740687,
};

var buf: [100]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&buf);
var string = std.ArrayList(u8).init(fba.allocator());
try std.json.stringify(x, .{}, string.writer());

try expect(eql(u8, string.items,
\\{"lat":5.199766540527344e+01,"long":-7.406870126724243e-01}
));
}

The JSON parser requires an allocator for javascript's string, array, and map types.

test "json parse with strings" {
const User = struct { name: []u8, age: u16 };

const parsed = try std.json.parseFromSlice(User, test_allocator,
\\{ "name": "Joe", "age": 25 }
, .{},);
defer parsed.deinit();

const user = parsed.value;

try expect(eql(u8, user.name, "Joe"));
try expect(user.age == 25);
}