1
\$\begingroup\$

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 "}}";
\$\endgroup\$
2
\$\begingroup\$

The code can be slightly improved, but it won't make much of a difference overall. This whole approach seems a bit weird. Why does the JSON file exist in the first place?

Here is the 'improved' version:

<?php

// read the json file on the server
$txt = file_get_contents("json");

// get the id of the application
$id = filter_input(INPUT_GET, "id", FILTER_SANITIZE_STRING); 
if (isset($id)) {
    // decode json
    $json = json_decode($txt, true);
    // remove all other ids from the list
    $appInfo = $json["ids"][$id];
    unset($json["ids"]);
    $json["ids"][$id] = $appInfo;
    // encode and return json
    echo json_encode($json);
}
else {
  // simply echo the content
  echo $txt;
}

I sanatized the input by using filter_input(). That's not really needed, but always a good idea. I also simplified the code a bit more.

This is only a tiny piece of code, so there's not much I can do. I have a feeling that more could be achieved if we knew why you do this in the first place. What is this JSON used for?

=============================================

I couldn't resist proposing the code below for the "filechanger".

<?php

// read the json file on the server
$txt = file_get_contents("json");
// decode json
$json = json_decode($txt, true);
// get all application information
$appInfos = $json["ids"];
// write the to separate files
foreach ($appInfos as $appId => $appInfo) {
    // set application info
    unset($json["ids"]);
    $json["ids"][$appId] = $appInfo;
    // create file name
    $filename = str_replace(" ", "_", $appId) . ".json";
    // encode and save json
    file_put_contents($filename, json_encode($json));
}
\$\endgroup\$
  • \$\begingroup\$ The Json exist to maintain the different applications from a server and to set customized content tailored to the individual applications \$\endgroup\$ – Emanuel Graf Apr 30 at 11:33
  • \$\begingroup\$ Yes, I got that, but, sorry for saying it like this, it is a bit vague. I do realize it is difficult to explain. But I could imagine having separate JSON files for each application. No need to go through PHP then. This is just an example, there could be many other ways of doing it better. \$\endgroup\$ – KIKO Software Apr 30 at 11:36
  • \$\begingroup\$ Then there would arise the following concerns: 1. what is more efficient: to have this script or to have 150 json files? 2. While having 150 files, maintaining the part that is needed by every application will become very hard (Unless I programm a "Filechanger"). Furthermore, the json can change 2 to 3 times a day, so having an agile way of doing this update task is crucial \$\endgroup\$ – Emanuel Graf Apr 30 at 11:39
  • \$\begingroup\$ The 150 JSON files will be more efficient, for sure. The part needed by every application could just be a separate JSON file. Each application then reads two file: the general file used by all applications and a file tailored to that application's needs. \$\endgroup\$ – KIKO Software Apr 30 at 11:41
  • 1
    \$\begingroup\$ Ah, yes, that is a good idea. I can support that completely. \$\endgroup\$ – KIKO Software Apr 30 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.