3

I'm using a Spring Boot Java backend and a Javascript frontend. The backend needs to fetch data from a db and provides it to the frontend.

The latest top hits on google provide me with tutorials which all propose the same: use a Spring REST API backend and provide the data to the frontend as a JSON via http get against your backend.

I don't understand why this is the favored approach. The idea of JSON is to provide a "human-readable text" (Wiki). For what does my backened need that?!

As a result, the JSON generated in my case is nearly 800kb, as all fields and values are human readable. If I change the structure and use placeholder values, the size would shrink to 100kb and my frontend could totally work with that.

But by doing so, I break the whole idea of using JSON as a transfer objects.

So why does this seem to be such a favored approach by the community? Is there something I missed? Do you perhaps know of a better approach or should I just deal with it?

2 Answers2

2

Why Json?

  1. It's simple to handle json with Javascript (it has built-in support).

    1.1. Actually there are tools in almost any programming language for the json support.

  2. It's simple to make messages forward and backward compatible with json.

  3. Since json is text, normal HTTP has a very good compression on it, so at the end you end up sending approximately 10% of the payload if it's large enough.

  4. Human readable makes your life simpler when you need to troubleshoot. (Did you ever try to troubleshoot protobuf?)


Does it have to be json?

No.


Now, you didn't ask it, but "sort of" asked it. Don't send all your data to the page, but instead divide your data in chunks, what a page needs to load and what your page needs later. If you balance it correctly, you can benefit from both worlds: human readable format and a fast loading page.

Tarlog
  • 10,024
  • 2
  • 43
  • 67
0

The idea of JSON is to provide a "human-readable text" (Wiki).

Yes, but...

I would focus on the JavaScriptObjectNotation. E.g. the Mozilla docs on the subject:

JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax.

The short answer is: you get some things for free. You may be interested in:

What is JSON and why would I use it

and

Sending JSON from backend to frontend


In terms of data size: I'm curious what your JSON format is, that you can go from 800kb to 100kb by changing to something else.

MyStackRunnethOver
  • 4,872
  • 2
  • 28
  • 42