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

69
Views
Parsing JSON file and changing it into a JavaScript string

main.js

    jsonReader('./source/locations.json', (err, data) => {
        if (err) {
            console.log(err)
            return
        }
        for (let i = 0; i <= 10; i++) {
            //console.log(data.towns[i].townName);
            locationsout += `Town ${i+1} : ` + stringify(data.towns[i].townName) + "\n";
        }
        interaction.reply({
            content: `\`\`\`TOWNS: \n${locationsout}\`\`\``,
        })
    })

So I'm taking data from a json file I have that looks like this

  "towns":[
    {
        "townId": 1,
        "townName": "Pallet Town",
        "canTravel": 1,
        "hasMart": 0,
        "hasCenter": 0,
        "hasGym": 0,
        "canFish": 1,
        "locationImage":"https://cdn2.bulbagarden.net/upload/7/77/Pallet_Town_FRLG.png"
    },

but with 10 more similar towns and I'm trying to be able to output a reply which requires a string but I'm having trouble accessing it and transforming it to a string that I can use in javascript. Currently with this code the error I'm facing is

TypeError: Cannot read properties of undefined (reading 'townName')

but I've also had it somewhat working when using only stringify(data.towns[i]) but that outputs the entire object which i dont want I just want the townName part So I also tried stringifying then parsing it into a javascript object like so const obj = JSON.parse(tname)using let tname = stringify(data.towns[i]) but that resulted in a

SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse ()

And I just don't know what to make of that.

about 4 years ago · Juan Pablo Isaza
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!