DEV Community 👩‍💻👨‍💻

Fábio M Jr
Fábio M Jr

Posted on • Updated on

How a single JSON file could become your entire code base

The Problem

Well, it is no news for us that developing software is expensive, and takes A LOT of time and effort, after all, we're talking about code, good code.

It is that kind of code which solves a problem, and it does so while being elegant and readable... But doing that is hard, and here is where we touch the surface of a difficult problem.

Good code is really expensive, and inaccessible for most people and companies.

Not only bad/faulty code often fails to solve the problem it should, it also becomes the own problem. I bet you once had to understand the incredibly messy class system of a legacy code base, just because you were tasked to fix a "simple" bug.

My theory

I have a fun personal theory, that all ever created about code can be somewhat traced back to the code accessibility problem, with the sole purpose of making it bit by bit a little easier.

People started using C so they do not need to move bytes manually so often. On the web, the rise of JQuery led to more and more complex and capable websites, which was a thing only to dream about. Then came Java, C#, ReactJS, tools like Docker, and uncountable others goodies to make our life better.

However, the "writing good code" problem still persists, though adapted to the current scenarios.

Honestly, I don't think this problem has a definitive solution. As our society and technology gets more capable and complex, so does our problems; but I do think we can do better to make software more accessible.

Making code Accessible and Cheaper

Now, what if such "good code" could be written only once, and all of us could freely use that code for our own purposes? What if we can chain multiple good codes together, while not needing to write boilerplate code for that?

Perhaps, even better, we could be writing only the absolutely necessary code for our use case.

Here's where that JSON comes in.

Code as Data

Before we begin, for "code as data" to exist, the following must be true:

If It is possible to represent any code as a piece of information, it is possible to organize it to compose features and functionality.

Luckily for us, data can represent anything.

Don't believe me? Try giving this a read:

  "variables": [{ "name": "highestAllowedNumber", "value": 3 }],
  "code": [
      "procedureName": "if",
      "boolean": {
        "procedureName": "higherThan",
        "input": "functionInput1",
        "targetValue": "highestAllowedNumber"
      "then": { "procedureName": "stdOut", "message": "too high!" },
      "else": { "procedureName": "stdOut", "message": "you're fine." }
Enter fullscreen mode Exit fullscreen mode

When the user input is higher than 3, we will get a message: "Too high!"

Now, perhaps you're asking yourself, what is the difference from this to regular code?

At first glance not too much, however, in practice they are fundamentally different. You cannot execute this data, but you can parse it into code (good code too!), then execute it.

Data as Code

If we think about it, a good part of programming is converting information of a business process into a language the computer can more readily work with.

Try making this simple exercise. Get that data from the last section and write it in JavaScript, then C#, then C++.

After completing it, I think you can somewhat visualize that we can tell a computer make this conversion for us.

Data and You Making Code Accessible

Oversimplifying for the sake of comprehension, let's say you've written the best, unrivaled, if statement there is, and it could be represented by the same data structure we saw above.

If we manage to get data which correctly represents our intentions, it can become the best code we have, while not even thinking about its implementation at all. For best results, make it open source.

How a single JSON file could become your entire code base

Right now, I bet the title is not that far from reality, comparing what it initially seemed to be for you. Well, actually, such thing already exists, and here's a WIP example.

This was made possible by using Meta-System, an open source software in which I had the pleasure of working on. It makes software accessible, while also providing you a way to contribute to such accessibility.

Check the Repository, and join the discord, where we talk about making the world of software more welcoming and less challenging.

Top comments (48)

zakrabe profile image
Zachary Rabe • Edited on

Please don't do this without typescript and strong types.

At first config objects seems like a great idea to simplify and write less repetitive code. However, leaning too heavily on them can lead to rigid janky code to handle new cases and edge cases.

The dev experience becomes terrible when your coworkers have to read through hundreds of lines of config object parsing methods.

The time you initially thought you'd save is lost again to debugging and trying to find some undocumented setting or side effect that's not working as you expect.

IMO config objects are best when they are simple, self documenting, and no more than one or two layers deep


zelcion profile image
Fábio M Jr Author

This is absolutely a great call! Given the amount of data that can be condensed on such objects, skipping one or 2 props will eventually happen. Having typescript (or any other type system) is a MUST.

FYI, Meta-System is made with typescript under the hood, and I'm glad I used it.

zakrabe profile image
Zachary Rabe

Nice! If your config objects are created and consumed strictly by software (which I assume is likely the case if you're doing language translation) then these problems probably won't put as much burden on developers.

My feelings mostly come from projects I've implemented where developers were intended to create the configs. Not having strong enough types as a POC lead to needing a big refactor to improve discoverability of features for developers.

Common case of a POC mutating from "hey this could save us time" into "all my time is spent maintaining and documenting" 🤣

paradoxu profile image
Leo Letto

You're 100% correct, the idea is nice and will definitely save some time at the beginning, but with time and team growth, it becomes quite a mess specially if there's no proper documentation on how to use such files, and no one documents their work... Right now I'm working for a company that uses this approach, with old javascript split in different repositories, it is really impossible to debug and sometimes I take days just to find the right piece of code to update.

leob profile image

Using typescript for this seems a very good idea indeed ... JSON-schema maybe?

And I can imagine that at some point you'd also want some sort of a graphical editor, so that you can edit a "model" in a meaningful way (MDD, model-driven development) ...

I must say that when I saw the big blob of JSON that's needed for even a pretty simple app it gave me a bit of a headache, I can't imagine that it will really be a joy to "program" like that. Some sort of higher-level editor support would alleviate that ... and the ability to split the JSON declaration into separate files or "components".

The way I could really see this working is that you'd use a JSON model to generate the repetitive, boiler plate kind of stuff within your app, then the more "interesting" logic would still be coded in a conventional programming language.

Well this does look like MDD (Model Driven Development) all over again.

zelcion profile image
Fábio M Jr Author

I can definitely say that we're on the same page :)
All of this is on the Meta-System's Roadmap, which you can check it here.

Thread Thread
leob profile image

I see it! "Meta-System configuration can be split in multiple files"

srmagura profile image
Sam Magura • Edited on

Yes, you can make a Turing-complete programming language whose syntax is a subset of JSON. Your if statement example demonstrates this nicely.

But what problem does that solve?

You can write any program as a JSON data structure, but you could write that same program in C# or JavaScript and it will be far more readable and succinct.

JSON can be used to represent just about any kind of data, including a computer program, but it's not optimized for this. Like JSON, C# and JavaScript can be viewed as formats for storing data — the difference being that C# and JavaScript were designed specifically to represent computer algorithms while JSON was not.

zelcion profile image
Fábio M Jr Author

Hey Sam, thanks for coming up with this!

Well, I do agree with you. For simple tasks, such as the if statement in the article, it surely can take up more space than we would usually need with our own and beloved usual programming, and this is a good sign of the development of good languages for writing such rules in an easy manner.

I feel like data cannot simply replace our traditional programming, this is impossible given that the data needs to be eventually resolved in code. However, the great usability of this comes when we can abstract good chunks of repetitive logic in a simple API. If we combine that with a predictable API across multiple modules/libraries, we have come up with a way to greatly reduce programming effort.

Now, whether we want to represent our logic with data or not is up to the developer, and it also requires a good understanding of the problem (and its complexity) we are trying to solve. If we have enough flexibility within the data model, or the code is organized enough, this is likely influence our decision.

mattthecuber profile image

Couldn't agree more

mindplay profile image
Rasmus Schultz

This is called an Abstract Syntax Tree (AST) but why would you want to write out raw syntax trees by hand?

If for some reason you needed an AST, you could probably create a real syntax for your language and have a compiler mode that emits the syntax tree as JSON.

You're kind of starting with the middle state of a language/compiler and working your way backwards. This idea is very much missing a "why".

virtuecoder profile image
Jacek Kolodziejczyk

The idea is to represent the code as data not to write the data by hand. Nor to read it in JSON format. This aproach enables projectional editing (term popularized by Intellij in their MPS) that has much more potential then traditional text editors.
Why? Because we can write the code once and generate multiple versions of binaries choosing implementation language that suites the best the target evironments with their constraints. For example runs in the browser and also super fast on the desktop.
I actually faced this when migrating a gui component from SWT (Java) to Javascript). It felt such a waste of time to rewrite everything.

mindplay profile image
Rasmus Schultz

But it's still a language.

What you put in the JSON is effectively just a serialized AST for a specific language. Yes, it's JSON, but it's not just JSON - the structures and values have syntax and form that goes beyond JSON, much the same as how source languages go beyond ASCII or Unicode.

Plenty of compilers transform the AST in the middle, applying optimizations and so forth - applying transformations to JSON isn't fundamentally different from applying transformations to an AST. It's a model, with a specific schema, and you transform it. The back-end of the compiler ultimately transforms the final AST into some other source language or binary form.

Why? Because we can write the code once and generate multiple versions of binaries choosing implementation language that suites the best the target evironments with their constraints. For example runs in the browser and also super fast on the desktop.

That just explains why we have high-level languages and compilers.

From what you've explained, all you're proposing is a high-level language with a syntax that happens to be a superset of JSON.

I mean, yes, you'd be able to parse it using a standard JSON parser - it would work sort of like a high-level lexer, but you would still need to build all of the remaining parts of a compiler. (And probably a highly specialized editor as well, since working with these bulky JSON structures in a text editor would be... less than appealing.)

jonrandy profile image
Jon Randy

I was going to say the same thing

skamansam profile image
Samuel C. Tyler

I worked at a multibillion dollar company that had a single Google sheet control software operations. It was done this way so finance, sales, and C-level emoyees could configure our platform. Even if it was only dev controlling it, the adapter code for it grew exponentially with each update request. It ended up being a nightmare for everyone and eventually the company restructured communication to avoid that mess in the future.

zelcion profile image
Fábio M Jr Author

Yeah, I think this emphasizes the problem of "good code" accessibility, It's hard even for a multi billion dollar company.

For this specific kind of issue though, I'd say the problem was with the lack of a predictable interface. Can't say for sure since I don't know the code...

decaruju profile image
Julien de Carufel

I love this concept, but it would benefit greatly from being more extensible and allow for higher level features. Client needs such as "Render a paginated, filterable, sortable list of objects" is the kind of need which absolutely should be programmable by config.

I think that when you start implementing loops and branching with your JSON, you've gone too far in a weird direction.

zelcion profile image
Fábio M Jr Author

You would love to see Meta-System in depth then! We made the core to be extensible as you wish, so you can implement such high-level features and use them in your JSON file. Come say hi at the discord, I love to discuss about this sort of things :)

jfullerco profile image

I’ve been moving one of my react projects to a similar predetermined or templated structure using firebase and a “Core” collection. Basically like a schema for your schema. It’s a lot of work upfront but definitely pays dividends in the long run not having to chase down irregularities with how your data interacts as your project grows or changes. Also very helpful with data relationships as changing how those relationships function or are defined is as easy as changing the Core schema.

Word of caution though, abstraction always comes with the temptation to further abstract and can become a time sink for no actual improvement. I know because I’ve ended up there at times. Don’t let that dessuade you though as I’ve learned an invaluable amount about Js and React data handling in the process.

Don’t play too hard!!

wintercounter profile image
Victor Vincent

I'm having a similar approach. I have a few predefined/builder components and I define layout and hierarchy in JSON. With a single instace of this codebase I serve 100+ completely different websites, with a 150k React bundle size, including styles!

zelcion profile image
Fábio M Jr Author

This is an amazing work! Keep up <3

saubi1993 profile image
saurabh kushwaha

For a complex and large code it will be never helpful. For smaller jobs or for product managers it can be helpful. When you already know the stuff why do you need so much simplification. Write good code with well documentation. That's is it.

jlopez788 profile image
Juan Lopez

I started tinkering with a similar concept for blazor. Write a bunch of json files to create components. Handles state fairly well. Much more form oriented than whole website type of thing. Allows some json to be injected so that users can create forms on the fly, validated and possibly passed to some api for processing.

raibtoffoletto profile image
Raí B. Toffoletto

So... I'm still failing to see why this isn't better?

function (functionInput1) {
const highestAllowedNumber = 3;
if (functionInput1 > highestAllowedNumber) {
console.log('too high!');
console.log('you're fine.')

Unless you are parsing common language to create a program (Function that takes an input number X; If X greater than 3 print ... otherwise print ... ) I don't think JSON is a better way to describe code to be created, They are much harder to read and get invalidatet if you forget a comma somewhere, or has an extra comma at the end of you properties.

Like you Fabio, I'm self-taught, and I make a great effort to constantly improve myself to write GOOD CODE, but this json can become very messy as written code can... so why the extra step? Or have you thought about another data structure to use as a base? YML would be much clear, but a wrong indentation and kaput ..

zelcion profile image
Fábio M Jr Author

Raí, you're absolutely right!

I'll start answering your last question: "Or have you thought about another data structure to use as a base?"

Yes, Meta-System uses another data structure, which I would love to show it to you, if you're willing so. Come say Hi at the discord link provided above :)

Now for the "why the extra step?". Well, one thing about data, is that we can more easily interact with it. Writing the JSON directly is hard? Well, we can then just have a drag and drop GUI for writing it for us. In the end, we're [sort of] writing code, but with a giant leap on accessibility.

antonello_ceravola_ccd8f4 profile image
Antonello Ceravola • Edited on

Nice article, and definitely you are on a point too little considered by the community that generate it.
I have touched code as data on a recent project. It may be interesting for you since it show another perspectives on that.
You may look at:

You may also have a look at an article on that:

rafaeld86848644 profile image
Rafael Dias

Awesome, I'll have a better look and join the Discord!

abykuruvilla profile image
Aby Kuruvilla

I cringed all the way to the end. Whatever you are trying to achieve exists as "magic" in many opinionated frameworks.
JSON is not expressive enough for whatever you mentioned. It will look ugly and unreadable while making anything meaningful.
I would have cringed less if it was say YAML.
There exists a lot of configuration driven plug and play frameworks, all of it starts to bite you in the ass while straying away from what the author wants you to do.
I use java and rely on opinionated frameworks along with open source libraries like apache commons lang to do even string comparisons.
This basically accomplishes the same thing with a window for pulling out big guns if you want to.

leob profile image
leob • Edited on

Reminds me a lot of the 4GL's of the 80s, or of "Model Driven Development" (and it sounds a lot like the "no code" or "light code" movement too) ...

But, I do see the value of the idea of 'automating away' the repetitive, boiler-plate like parts of coding/development, and of replacing "imperative programming" by "declarative programming".

The point is - this has been tried in so many different ways, shapes and forms (see my opening sentence), but reality is we're still writing code "in the old way".

But, never say never, who knows how we'll construct software in 10 or 15 years from now - I can't really imagine that we'll be doing it exactly as we are doing it now, by endlessly coding our React hooks and components and whatnot - I do expect a large part of it to be done somehow in a more "high level", abstracted, declarative, model-driven way.

mrispoli24 profile image
Mike Rispoli

Oh I'm working on something very similar myself but a slightly different spec than you have here. I almost think of it more like js arrays written as a LISP. Very similar concept just written with less keys etc and using functional paradigms.

Need a better mental model for async/await?

Check out this classic DEV post on the subject.

⭐️🎀 JavaScript Visualized: Promises & Async/Await

async await