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

181
Views
Access JSON object from another file in Javascript

Perhaps a simple question but I am still new to JS / nodeJS.

I have a script that is doing some basic string matching (dict.js), and I am trying to access a JSON formatted object from another file (words.json) to iterate through.

The directory structure looks like:

scratch
- algorithms
  - dict.js
- utilities
  - words.json

The contents of the files are:

words.json

{"a": 1,"aa": 1,"aaa": 1,"aah": 1,"aahed": 1,"aahing": 1,"aahs": 1,"aal": 1}

dict.js

decode (password) {
    
    const jsonData = require('../utilities/words.json'); 
    myObj = JSON.parse(jsonData);

    for (const x in myObj) {
        console.log(x)
        // compare password against item in words.json
    }
    console.log(Object.keys(myObj).length)

    return "stub";
}

I am getting an error in developer tools when I create a block (this is the backend to a block in Scratch) that uses this Decode function.

Uncaught SyntaxError: Unexpected token o in JSON at position 1

Thanks

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

0

In nodejs environment you can directly import json file data without parsing it. For exaple:

const myObj = require('../utilities/words');

This gives you the data in your json file as a ready-to-go object.

In your case, you are trying to parse json object with JSON.parse() which expects stringified json. So just remove the part JSON.parse(jsonData);

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!