php - Load JSON file from URL into laravel, then create a search that can access that data

I am new to Laravel and on a time crunch for a project. Looking for any help on getting started with this project. I need to import this table from this url http://ftp.ebi.ac.uk/pub/databases/genenames/hgnc/json/locus_groups/protein-coding_gene.json into laravel. First question is where to put this in the file structure.

Then, I need to create a search that can access just 2 parts of this table (gene symbol and location) and display the results. What are some basic setup tips and/or code to help me access that data from my search bar?

Thank you so much!

I know this code will get me started:

public function index()
    $results = file_get_contents("http://ftp.ebi.ac.uk/pub/databases/genenames/hgnc/json/locus_groups/protein-coding_gene.json");
    $data = json_decode($results, true);

But not sure where to put that and where to go from there.

Answer

Solution:

First question is where to put this in the file structure.

Up to you, also depends on if you're downloading it on the fly each time your code runs, or if you're uploading it to your Laravel app as a static asset.

If you're downloading on the fly, I'm not sure that you do need to store it on your filesystem really. If it's an asset which you need to manually update, upload it to public/assets or similar.

If you want to use it for searching, you'll probably need to store the file as an asset and load it into your app.

Then, I need to create a search that can access just 2 parts of this table (gene symbol and location) and display the results. What are some basic setup tips and/or code to help me access that data from my search bar?

I'd suggest using a JSON parser like https://jsonformatter.org/json-parser to format the file and explore its structure. Using your IDE's autoformatter is also fine.

$data is just an associative array so you can access its contents as normal. For searching, you can load up your JSON file and loop through the array, check if the current object's name/location matches the search query according to your searching method (equality, startsWith, etc). It looks like your JSON file is pretty large though, and while looping probably won't take too long, loading that all into memory might not be nice. So you probably want to maintain some sort of cache, or parse the datafile into a local database, and write up an SQL query for it.

If you don't want to spin up a database etc, a more lightweight approach might be to process the JSON file once, create a new smaller JSON file which just contains the search criteria and an array index reference to where the object lies in the bigger JSON file.

Source