Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jeroenvanrensen's avatar

JavaScript - Reading JSON from text file

Hi everyone,

I'm learning JS and I found a free Google API:

google.com/complete/search?client=firefox&q=laravel

If I visit this URL I got a text file download.

My question is: How can I translate this file into a JS-object using only Pure JS?

Thank you! Jeroen

0 likes
15 replies
jeroenvanrensen's avatar

@michaloravec yeah, I know about that. But how could I get it from a text file?

Can I just do this?

JSON.parse('google.com/complete/search?client=firefox&q=laravel');
jeroenvanrensen's avatar

Hi @michaloravec,

But how can I implement that?

If I send an AJAX request to that URL, do I then get the JSON output?

So, does this work:

axios.get('google.com/complete/search?client=firefox&q=laravel')
.then(function (response) {
	console.log(response);
  });
jlrdw's avatar

Does if have to be javascript? Pretty easy in PHP, you could get data in array and json encode. Just asking.

jeroenvanrensen's avatar

Hi @jlrdw,

I need the data in JavaScript, but if it’s easier in PHP I of course could make an AJAX request every time.

Could you give me a code snippet how I can get it in PHP?

Thank you! Jeroen

jlrdw's avatar

The following code allows to import a csv, I don't get all fields, only needed a few And looks like:

Code:

    public function incsv() {
        $file = 'C:\mydocs\incsv.csv';
        $content = file($file);
        $array = array();

        for ($i = 1; $i < count($content); $i++) {
            $line = explode(',', $content[$i]);
            for ($j = 0; $j < count($line); $j++) {
                $array[$i][$j + 1] = $line[$j];
            }
        }

        $k = count($array) + 1;

        for ($i = 1; $i < $k; $i++) {
            $tdate = new \DateTime($array[$i][2]);
            $ndate = $tdate->format('Y-m-d');
            $descraw = $array[$i][8];
            $descspace = preg_replace('/[^a-z\d ]/i', '', $descraw);
            $desc = preg_replace('/^\s+|\s+$|\s+(?=\s)/', '', $descspace);
            $amt = (float) $array[$i][3];
            $namt = number_format($amt, 2, '.', '');
            if ($namt < 0) {
                $wd = $namt * -1;
                $dep = 0;
            } else {
                $wd = 0;
                $dep = $namt;
            }

            $maxid = $this->Check->getMaxid();
            $checkid = $maxid + 1;

            $data = [
                'checkid' => $checkid,
                'transdate' => $ndate,
                'transdescribe' => $desc,
                'widthdraw' => $wd,
                'deposit' => $dep
            ];
            $this->Check->insertCsv($data);
        }

        $this->Check->checkRecalc();
        Url::redirect('check/index');
    }

I included all code, but you can dd and see what different parts are doing and just get the array part prior to json_encode.

A csv needs to be consistant.

Edit: This was not done in laravel, but another framework, but hopefully you will get the idea.

Also the regex was used to strip unwanted characters and extra space.

Some transaction might look like:

*** Some transaction ***    Other text etc 

I only wanted
Some transaction Other text etc

If used, adjust as needed. I don't get the first row (header) it's already set in the table.

cuwa's avatar

Good lord that code is painful to look at. I feel bad for other programmers who might come across any of your code or have to maintain your code.

jlrdw's avatar

@cuwa could you please share some code that would help @jeroenvanrensen.

That code imports fine and would only need tweaking if the Bank changed format of the csv.

@jeffreyway please inform folks to try and be helpful, as you know @jeroenvanrensen doesn't have to use any code from forum.

@cuwa look at the vendor code for some of the csv packages, how do you think this stuff is done in the background. Now please help @jeroenvanrensen and share your code nicely, or move on.

jlrdw's avatar

A package that might help

https://github.com/Maatwebsite/Laravel-Excel

Just remember in these packages it might be possible to do things in two or three lines of code.

But definitely look at the vendor folder and you will see hundreds of lines of code that makes that 2 or 3 lines of code possible.

jeroenvanrensen's avatar

Hi @cuwa and @jlrdw,

I guess you misunderstood me. I'm not using Laravel, and I just want to know how I can get the value of a text file, because the problem here is that I don't get an JSON page, but a text-file download.

It doesn't matter if it's done with JavaScript or PHP, because if it's done with PHP I could make an Ajax request.

I hope you know how to do this.

Thank you! Jeroen

kundefine's avatar
Level 2
axios.get('google.com/complete/search?client=firefox&q=laravel')
.then(function (response) {
	console.log(response.data); // it will give you the javascript Array or Object if it is a JSON file
        console.log(JSON.stringify(response.data)); // if you want it as string you can just do this
  });

Please or to participate in this conversation.