On my server I have a large json that is accessed by several applications. One application only needs a certain part of the json, so presenting the whole json to the application is an unnecessary increase of bandwith. (Original JSON: about 40 KB, Optimized JSON: about 500 b)
What I came up with is a php script to which every application sends the application name. The script then reads the json on the server and prints out the shrinked json for the application to read. It works, but I'm concerned about wheter the php script could possibly put the server processing capabilities to its limits. Is there a more efficient solution over mine?
Note: The part before the "ids" object is needed by all applications, that's why the php script just echo it out. If an application fails to send its name to the server, the server just outprints the raw json.
The raw JSON as it exists on the server:
{
"a": 0,
"d": 0,
"pD": "ok",
"p": "ok",
"an": "123",
"slider_1": ["One Slider", "https://example.com/1"],
"slider_2": ["Two Slider", "https://example.com/2"],
"ids": {
"Application One": {
"info one": "210799155",
"info two": "1544002021777"
},
"Application Two": {
"info one": "211061341",
"info two": "0000000",
},
.
.
.
much more Applications
The PHP script to optimize:
<?php
//Read and decode the json file on the server
$txt = fread(fopen("json", "r"),filesize("json"));
$json =json_decode($txt,true);
//Echo the part needed by every applicaition
echo '{
"a": 0,
"d": 0,
"pD": "ok",
"p": "ok",
"an": "123",
"slider_1": ["One Slider", "https://example.com/1"],
"slider_2": ["Two Slider", "https://example.com/2"],
"ids": { "' . $_GET["id"] . '" :';
$s = json_encode($json["ids"][$_GET["id"]]);
//check if the application failed to send the name
if ($s === "null"){
ob_end_clean();
echo $txt;
exit();
}else{
//write the application specific part of the json
echo $s;
}
echo "}}";