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

Kapygon's avatar

Best way to use big JSON file

Hello, I have a big JSON file (about one million lines). The goal is to convert Japanese postcodes to the address, hence I do not need to store the full address in the database, and it's simpler for me to display it in Japanese or Alphabet.

But when I want to load it from my controller, I get an error 500. So I suspect the file is too big (actually the error happens when I try to decode the file).

Then I created a Javascript class (see below) that loads the file within asyn function. So I do not get an error here. However, I can access my object only from app.js, even if I add it to the window variable (see below).

So I have two questions:

  • How to access a class I loaded from the app.js in my project?
  • What is the best way to load this file (by JS or PHP)?

Here is what I did with JS:

// inside resources/js
export default class PostalCodeConvertor {
    constructor(postalCode=0) {
      this.postalCode = postalCode;
      this.data = null;
    }

    // Loading data from JSON
    async loadData(postalCode=-1) {
      try {
        const response = await fetch('/japan_postal_code.json'); // inside public
        if (!response.ok) {
            console.log("Not loading file");
          throw new Error('Couldn\'t load the postal code data.');
        }
        const jsonData = await response.json();
        if(postalCode != -1)
            this.postalCode = postalCode;
        this.data = jsonData[this.postalCode] || null;
      } catch (error) {
        console.log("error code");
        console.error('Error while loading the postal code:', error);
      }
    }

    // Getters
///...

Here is the code inside app.js with some display of the data (that is working in this file)

import PostalCodeConvertor from './PostalCodeConvertor.js';
window.PostalCodeConvertor = PostalCodeConvertor;

var postalCode = new PostalCodeConvertor("0600042");
postalCode.loadData().then(() => {
    console.log('Préfecture (kanji):', postalCode.getPrefKanji());
    console.log('Ville/Quartier (kanji):', postalCode.getCityAreaKanji());
    console.log('Rue (kanji):', postalCode.getStreetKanji());
    console.log('Préfecture (roman):', postalCode.getPrefRoman());
    console.log('Ville/Quartier (roman):', postalCode.getCityAreaRoman());
    console.log('Rue (roman):', postalCode.getStreetRoman());
  });

But when I use it inside app.blade.php, it fails

<!-- ... -->
<head>
@vite(['resources/css/app.css', 'resources/js/app.js'])
</head>
<!-- ... -->
script>
var postalCode = new window.PostalCodeConvertor("0600042");
</script>

Uncaught TypeError: window.PostalCodeConvertor is not a constructor

0 likes
6 replies
krisi_gjika's avatar

"window.PostalCodeConvertor is not a constructor" the error tells you everything. window.PostalCodeConvertor is an instance of the class, not a constructor so you can't "new" it. Try something like window.PostalCodeConvertor.loadData("0600042"), basically have one instance of the class handle any postal code.

"What is the best way to load this file (by JS or PHP)?" - depends on your needs, you can use something like: https://github.com/halaxa/json-machine or https://github.com/cerbero90/json-parser to load your big json line by line in php.

Kapygon's avatar

@krisi_gjika Whe I do this I got this error: Uncaught TypeError: Cannot read properties of undefined (reading 'loadData') Even if I changed the creation of the object in the app.js by this:

window.PostalCodeConvertor = new PostalCodeConvertor();

Thanks for the links, I will have a look!

Kapygon's avatar

@Tray2 So far only one time, when I display the data (and maybe when I record them), I have several businesses recorded and each time I display one of them, I want to translate the postcode to the address. But since the postal code and address are something common, I might need them on other pages.

Thanks for the idea. I guess it's because it's much faster/more efficient to access the database than parsing the JSON file? So Maybe I can create a job to do that, from the original CSV file from Japan Post (I converted it myself to the JSON).

Please or to participate in this conversation.