Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

70
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda