# Is possible to generate classes by a macro that loads and parse JSON definition?

**URL:** https://forum.crystal-lang.org/t/is-possible-to-generate-classes-by-a-macro-that-loads-and-parse-json-definition/2680
**Category:** Help & Support
**Created:** [November 16, 2020, 12:01am UTC](https://forum.crystal-lang.org/t/is-possible-to-generate-classes-by-a-macro-that-loads-and-parse-json-definition/2680 "2020-11-16T00:01:30Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![pfischer](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/pfischer/32/330_2.png) [@pfischer](https://forum.crystal-lang.org/u/pfischer)
#### Post date: [November 16, 2020, 12:01am UTC](https://forum.crystal-lang.org/t/is-possible-to-generate-classes-by-a-macro-that-loads-and-parse-json-definition/2680/1 "2020-11-16T00:01:30Z")

</div>

Hello! Is possible to make macro, that loads JSON, parse it and generate classes based on this JSON recipe? I know, that I can load file by the macro - is JSON parsing completely out of the concept of macros? Thanks!

---

<div class="post-metadata">

### Author: ![oprypin](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/oprypin/32/859_2.png) [@oprypin](https://forum.crystal-lang.org/u/oprypin)
#### Post date: [November 16, 2020, 1:15am UTC](https://forum.crystal-lang.org/t/is-possible-to-generate-classes-by-a-macro-that-loads-and-parse-json-definition/2680/2 "2020-11-16T01:15:12Z")

</div>

1. It’s always possible to achieve this through any build system (_Makefile_ etc.)
2. In Crystal any code generation is possible through [macro `run`](https://crystal-lang.org/api/0.35.0/Crystal/Macros.html#run(filename,*args):MacroId-instance-method). Just make a program that prints source code “normally”. This is quite heavy, though, and I don’t think it’s parallelized (but it _is_ cached).

---

<div class="post-metadata">

### Author: ![jgaskins](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jgaskins/32/2449_2.png) [@jgaskins](https://forum.crystal-lang.org/u/jgaskins)
#### Post date: [November 17, 2020, 12:01am UTC](https://forum.crystal-lang.org/t/is-possible-to-generate-classes-by-a-macro-that-loads-and-parse-json-definition/2680/3 "2020-11-17T00:01:22Z")

</div>

I wonder if it would make sense to translate the JSON schema to Crystal outside of the compilation step, similar to how Protobuf compiles from its source format into language-specific code in a separate step using the `protoc` tool.

This way you can see exactly what Crystal code you’re generating. That generated code might use macros but it can be a lot easier to read. I’ve never found JSON schemas written in JSON to be very readable as source material, though. 🙂

---

<div class="post-metadata">

### Author: ![sol.vin](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/sol.vin/32/73_2.png) [@sol.vin](https://forum.crystal-lang.org/u/sol.vin)
#### Post date: [November 17, 2020, 4:40am UTC](https://forum.crystal-lang.org/t/is-possible-to-generate-classes-by-a-macro-that-loads-and-parse-json-definition/2680/4 "2020-11-17T04:40:15Z")

</div>

Is this what you are looking for?

> **[Crystal JSON beyond the basics - lbarasti's blog](https://lbarasti.com/post/json_beyond_basics/)**
>
> In this article, we’ll look at how we can encode and decode Algebraic Data Types (ADTs) in Crystal using the json module and its powerful macros.
