Read and output a json object with the least server processing expenditure possible 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:

&lt;?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 "}}";
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:

&lt;?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".

&lt;?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 =&gt; $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));
}