-
-
Save morisono/b9c7962f1de84e3900c83b3ad9d59652 to your computer and use it in GitHub Desktop.
n8n-json-schema-from-sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * n8n Code node — Generate a JSON Schema from a sample JSON | |
| * - Paste this into a Code node (JavaScript) | |
| * - Set `rawInput` to where your example lives | |
| * - Toggle `allFieldsRequired` as needed | |
| */ | |
| // 1) WHERE is your example JSON? | |
| // - If you already have an object, set it to that. | |
| // - If you have a stringified JSON, set it to the string. | |
| // - Adjust the path as needed. | |
| const rawInput = $('YOUR INPUT'); // <-- change to your field (e.g. $input.first().json.Output) | |
| // 2) OPTIONS | |
| const allFieldsRequired = false; // true => mark present keys as required | |
| // ---- helpers ---- | |
| function safeParse(x) { | |
| if (typeof x !== "string") return x; | |
| const s = x.trim(); | |
| if ((s.startsWith("{") && s.endsWith("}")) || (s.startsWith("[") && s.endsWith("]"))) { | |
| try { return JSON.parse(s); } catch { /* ignore */ } | |
| } | |
| return x; | |
| } | |
| function isInteger(n) { return Number.isInteger(n); } | |
| // ---- core generator (extracted & simplified) ---- | |
| function generateSchemaFromExample(exampleJsonStringOrObject, allReq = false) { | |
| const sample = typeof exampleJsonStringOrObject === "string" | |
| ? JSON.parse(exampleJsonStringOrObject) | |
| : exampleJsonStringOrObject; | |
| function walk(value) { | |
| if (value === null) return { type: "null" }; | |
| if (Array.isArray(value)) { | |
| // LIST MODE: Infer items from the FIRST element (common heuristic) | |
| // To switch to TUPLE MODE (schema per index), replace with: | |
| // return { type: "array", items: value.map(walk) }; | |
| if (value.length === 0) return { type: "array", items: {} }; | |
| return { type: "array", items: walk(value[0]) }; | |
| } | |
| const t = typeof value; | |
| if (t === "string") return { type: "string" }; | |
| if (t === "number") return isInteger(value) ? { type: "integer" } : { type: "number" }; | |
| if (t === "boolean") return { type: "boolean" }; | |
| if (t === "object") { | |
| const properties = {}; | |
| const required = []; | |
| for (const [k, v] of Object.entries(value)) { | |
| properties[k] = walk(v); | |
| if (allReq) required.push(k); | |
| } | |
| const out = { type: "object", properties }; | |
| if (allReq && required.length) out.required = required; | |
| return out; | |
| } | |
| // fallback | |
| return { type: "string" }; | |
| } | |
| return walk(sample); | |
| } | |
| // ---- run ---- | |
| const sample = safeParse(rawInput); | |
| if (sample === undefined) { | |
| throw new Error("Sample JSON is undefined. Adjust the `rawInput` path in the Code node."); | |
| } | |
| const schema = generateSchemaFromExample(sample, allFieldsRequired); | |
| // Return BOTH object and pretty string for convenience | |
| return [{ | |
| json: { | |
| schema, // JSON object (best for passing onward) | |
| schemaText: JSON.stringify(schema, null, 2), // Pretty string (for saving/printing) | |
| } | |
| }]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment