Jsonc support #580
Answered
by
sdorra
JonathonRP
asked this question in
Q&A
Jsonc support
#580
-
|
Does the lib support jsonc? |
Beta Was this translation helpful? Give feedback.
Answered by
sdorra
May 28, 2025
Replies: 1 comment 1 reply
-
|
There is no build-in jsonc parser, but you can use a custom parser e.g.: import {
defineCollection,
defineConfig,
defineParser,
} from "@content-collections/core";
import JSONC from "comment-json";
import { z } from "zod";
const parser = defineParser((content: string) => {
const ast = JSONC.parse(content);
if (ast === null || Array.isArray(ast) || typeof ast !== "object") {
throw new Error("Invalid content only objects are allowed");
}
return ast;
});
const movies = defineCollection({
name: "movies",
directory: "movies",
include: "*.jsonc",
parser,
schema: z.object({
title: z.string(),
description: z.string(),
year: z.coerce.number().min(1888).max(new Date().getFullYear()),
}),
});
export default defineConfig({
collections: [movies],
});The example above uses the comment-json library, which must be installed first. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
JonathonRP
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is no build-in jsonc parser, but you can use a custom parser e.g.: