How to reduce a JSON request in half.

I was developing a Palworld breeding calculator, well I got into a few issues, so I wanted to share.

1- Multiple requests for the same thing.

Let’s say you wanted to make the same Pal breeding calculator I did just a mined CSV chart dumped into a .json, so how to handle every 111 pal + every child into a .json? first of all, in Python, you have to mine the CSV file, after mining it and turning it into a JSON like this:

[
    {
        "_lin_": "Lamball",
        "1": "Lamball",
        "2": "Lamball",
        "3": "Mau",
        ...
    },
    ...
]

So this is giving me the father and child combos, but how could I get from a child all its possible parents? I would need to take 2 loops and look for each father and mother for a child. Or simply make another .json and gather it from there? well, I did both to reduce data usage (by more than half).

After I got this .json request with the gathered data, I simply optimized the maximum possible to gather father+mother = child data and make another .json on a .json variable, but this time it was a child for fathers. and TADA! both the father’s calculator and the child’s calculator!

2 – Useless data.

Let’s say you have a .json response, where you need to send a message in multiple formats, but you don’t know which or have to use multiple alternative words, like this:

{
   "Mobile_title": "Welcome!",
   "PC_tile": "Welcome!",
   "mobile_desc": "this is a mobile notification",
   "PC_desc": "this is a pc notification",
}

Congratulations! You used the double you had to use. Let’s see how to fix this, shall we?

{
   "title": "Welcome!",
   "desc": "something to say until {data}",
}

Then, on javascript, you just replace {data} with the actual Something, instead of giving useless information, obviously, it just removed a few bytes, but if you had something like the .json I was working on… well.

3- Only request when needed.

This sounds pretty obvious but, it’s good to point out that probably a lot of people still do this to this day, let’s say you have a gallery on an image website, instead of giving 1.000 URLs in the same request, why not make it an “infinite” scroll? it is kinda simple, on your back end you have to take an ordering list of files, for example, sort it by name or date with the Python back-end, and then give back 25 images URLs, scroll more, hit bottom, and make the request again. here is a simple script example to do it:

# A list of strings, somehow your list of images in the gallery (os.listdir or even a db)
mylist = ["world", "LearnPython.com", "pineapple", "bicycle"]

# Using sort() is pretty bad so use the other one
mylist.sort()
print(mylist) # ['LearnPython.com', 'bicycle', 'pineapple', 'world']

# Using sorted() to return a new sorted list, key makes a condition, this being everything lower case
newlist = sorted(mylist, key=str.lower)
print(newlist) # ['bicycle', 'LearnPython.com', 'pineapple', 'world']

Leave a Comment