1

This is a dilemma I have been chipping away at. I can't seem to figure out the best approach here.

I have my server pull multiple json files to one single directory and the json files are all in the same structure. Each of the files have a randomly generated file name.

My end goal is to append certain elements of each json file to an easily readable html table.

Here is my current code that works great with a single json file:

$.getJSON('testing1.json, function(data) {
$.each(data.user.products, function () {
  $("table").append($("<tr>").append(
    $("<td>").addClass("Title").text(this.title),
    $("<td>").addClass("Price").text("$"+this.price),
    $("<td>").addClass("Stock").text(this.stock),
  ));
});
});

Now I need to figure out the best way to go about my issue. I have ~250 json files I need to loop through. Obviously this regex wont work but may give you an idea of what I am trying to do:

$.getJSON('*.json, function(data) { 

I would prefer to not have to merge them all into one single file. But the fact all the names are randomly generated, and I don't know how to load multiple json files into my code, I am worried I will have to do this. But it wouldn't be the end of the world if this was the case.

Is it possible to use Jquery/PHP to loop through the directory and pull all the json files? If so how would one go about loading each json file to append data from?

I really appreciate the help in advance, even a push in the right direction is much appreciated.

Jason Waltz
  • 435
  • 2
  • 10
  • Are you using a server? If so, does your web server have directory browsing enabled? – AskYous Mar 01 '20 at 03:42
  • Yes and yes to both of your questions. – Jason Waltz Mar 01 '20 at 03:54
  • What server side language is being used? NodeJS? PHP? – AskYous Mar 01 '20 at 04:24
  • It is a PHP server. But I can host any language. – Jason Waltz Mar 01 '20 at 04:25
  • You should use the server language to [retrieve all files in a folder](https://stackoverflow.com/a/15774702/1404347). Then go through all of them to create a big json. Then the front end would make an http call to that back end url. Also, you may want to look into supporting pages. You don't want a heavy page, slowing users browsers down. – AskYous Mar 01 '20 at 22:12

2 Answers2

0

Make a .php file and make a call to this file. In this file use php methods to load all the files starting let's say with word json from the specified directory. That will allow you in 1 request load all files back.

Like an example, this will give a good starting point:

/**

 * load all site-wide jscript_*.js files from includes/jscript, alphabetically

 */

  $directory_array = $template->get_template_part($template->get_template_dir('.js',DIR_WS_TEMPLATE, $current_page_base,'jscript'), '/^jscript_/', '.js');

  while(list ($key, $value) = each($directory_array)) {

    echo '<script type="text/javascript" src="' .  $template->get_template_dir('.js',DIR_WS_TEMPLATE, $current_page_base,'jscript') . '/' . $value . '"></script>'."\n";

  }

//Load all JS files and here are the main funtions:

function get_template_part($page_directory, $template_part, $file_extension = '.php') {
    $directory_array = array();
    if ($dir = @dir($page_directory)) {
      while ($file = $dir->read()) {
        if (!is_dir($page_directory . $file)) {
          if (substr($file, strrpos($file, '.')) == $file_extension && preg_match($template_part, $file)) {
            $directory_array[] = $file;
          }
        }
      }

      sort($directory_array);
      $dir->close();
    }
    return $directory_array;
  }

  function get_template_dir($template_code, $current_template, $current_page, $template_dir, $debug=false) {
    //  echo 'template_default/' . $template_dir . '=' . $template_code;
    if ($this->file_exists($current_template . $current_page, $template_code)) {
      return $current_template . $current_page . '/';
    } elseif ($this->file_exists(DIR_WS_TEMPLATES . 'template_default/' . $current_page, preg_replace('/\//', '', $template_code), $debug)) {
      return DIR_WS_TEMPLATES . 'template_default/' . $current_page;
    } elseif ($this->file_exists($current_template . $template_dir, preg_replace('/\//', '', $template_code), $debug)) {
      return $current_template . $template_dir;
    } else {
      return DIR_WS_TEMPLATES . 'template_default/' . $template_dir;
      //        return $current_template . $template_dir;
    }
  }

  function file_exists($file_dir, $file_pattern, $debug=false) {
    $file_found = false;
    $file_pattern = '/'.str_replace("/", "\/", $file_pattern).'$/';
    if ($mydir = @dir($file_dir)) {
      while ($file = $mydir->read()) {
        if (preg_match($file_pattern, $file)) {
          $file_found = true;
          break;
        }
      }
      $mydir->close();
    }
    return $file_found;
  }
Serghei Leonenco
  • 3,478
  • 2
  • 8
  • 16
  • This is an interesting take I would have never thought of. I was also thinking about making an sql database as well but this solution seems more straight forward to me. If I understand correctly, I just make a getJSON call to the php file? I didn't know you could do this. I will give this a try now. Thanks – Jason Waltz Mar 01 '20 at 03:42
  • Yes you can, and that will helps to load all the files – Serghei Leonenco Mar 01 '20 at 04:00
  • Still can't seem to figure this out, but I appreciate the push in the right direction. I will keep working on it. Thanks – Jason Waltz Mar 01 '20 at 04:35
  • You are welcome. This method is widely used in shopping carts to load any additional js, css or any other files which are not stored in db and has to be loaded or returned in the request. – Serghei Leonenco Mar 01 '20 at 04:38
0

Using glob I was able to scan and merge all .json files. Although not really ideal, it works for the most part.

<?php
$files = glob("*.json");
$newDataArray = [];
foreach($files as $file){
    $thisData = file_get_contents($file);
    $thisDataArray = json_decode($thisData);
    $newDataArray[] = $thisDataArray;
}
$newDataJSON = json_encode($newDataArray);
file_put_contents("merged.json",$newDataJSON);
?>  
Jason Waltz
  • 435
  • 2
  • 10