How to build a custom UI (consuming JSON)
If you are building a modern web application (e.g., using React, Vue, Svelte, or Next.js), you should consume Gram’s compiled JSON data directly and map it to your own UI components.
While the raw output of the parser is an Abstract Syntax Tree (AST), the @gram-lang/kitchen and @gram-lang/analyzer packages transform this AST into a highly structured, render-ready JSON object. This guide shows you how to programmatically extract this JSON data and understand its structure so you can build anything you want.
-
Getting the JSON data
Section titled “Getting the JSON data”Gram is designed to run anywhere (Node.js, Edge, or directly in the browser). To get the fully enriched JSON representation of a recipe, you compose the three core packages:
-
Understanding the JSON structure
Section titled “Understanding the JSON structure”The returned JSON object represents the complete, structured recipe. It contains everything you need to build a UI.
Here are the most important fields in the root object:
title(string): The name of the recipe (from the# Header).meta(object): Key-value pairs for metadata (e.g.author,yield).shopping_list(array): An aggregated list of all ingredients across all sections.sections(array): The actual instructions, broken down by recipe section.
Inside a section
Section titled “Inside a section”Each
sectionin thesectionsarray has its owningredients,cookware, andsteps. -
Rendering logic
Section titled “Rendering logic”Because Gram’s JSON payload is highly structured, building a UI is a matter of writing simple loops or
mapfunctions in your frontend framework.Example: rendering steps in React
Section titled “Example: rendering steps in React”Here is a conceptual example of how you might render the
stepsarray in React:By mapping the
typeproperty to your own components, you have total control over the design, interactivity, and accessibility of your recipe application!