With JSON.parse()
Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
With JSON.parse()
Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
@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');
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);
});
Does if have to be javascript? Pretty easy in PHP, you could get data in array and json encode. Just asking.
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
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.
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.
@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.
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.
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
See https://stackoverflow.com/questions/27979002/convert-csv-data-into-json-format-using-javascript but there are other stackoverflow articles.
When you mention text file, you do mean csv, correct, or is it already in json format.
But also search stackoverflow.
Hi @jlrdw,
I mean: I want to get the content of that text file as a string in JavaScript. But the problem is that it isn't just a page, but a text file download.
You can try it yourself using this link: google.com/complete/search?client=firefox&q=laravel
And I want to get it in JavaScript or PHP.
Jeroen
Okay now I see what you need. See my reply here https://laracasts.com/discuss/channels/laravel/laravel-hierarchy
Where I showed how to convert to array and work with the array. You could even use collections.
But just use the Json decode like I showed in example in that link.
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.