Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
Problem with import json to variable in React

I'm new in React and I'm trying to import and save in variable a selected JSON data

[...]

const languageToSet = "polish";
const lang = {
    promiseToSetLanguage: function(lang){
        return new Promise((resolve, reject) => {
            import language from `./languages/${languageToSet}_lang.json`
            language = JSON.parse(language)
            if (language.welcome) {
                console.log('Works fine')
                resolve(true)
            } else{
                reject(false)
            }
        })
    }
}
lang.promiseToSetLanguage()

[...]

Console throw me a error: "'import' and 'export' may only appear at the top level" but i need to load a JSON file selected by user. I used 'import' and 'require' method.

Thanks for your help, best regards.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think that fetch might be the answer here.. Try to use

return new Promise(async (resolve, reject) => {
            const rawJsonFile = await fetch(`${file_from_user}`);
            const jsonFile = await rawJsonFile.json();
            if (jsonFile.welcome) {
                console.log('Works fine')
                resolve(true)
            } else{
                reject(false)
            }
        })

If it still wont work, you can always use xhr

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!