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

139
Views
Update JSON with express

I would like to send json updated on post response with express. Currently on my response the json is send but without to be updated.

Below function to edit json:

function editJSON(fr,en,es){
    var obj = {
        list_words: []
    }
    fs.readFile('./assets/words.json', 'utf-8', (err, jsonString) =>{
        if(err){
            console.log(err)
        }else {
            try{
                obj  = JSON.parse(jsonString)
                obj.list_words.push({fr: fr, en: en, es: es})
                json = JSON.stringify(obj)
                fs.writeFile('./assets/words.json',JSON.stringify(null, 2),function (err){
                    console.log("test")
                    if (err) throw err;
                    console.log('complete');
                })
            } catch (err){
                console.log(" Error parsing JSON", err)
            }

        }
    })
}

and post request

const words = require('./assets/words.json')
app.post(
    '/addwords',
    (req,res)=>{
        const errors = validationResult(req);
        console.log(req.body)
        const fr = req.body.fr
        const en = req.body.en
        const es = req.body.es
        editJSON(fr,en,es)
        console.log(words)
        res.json(words) // old JSON is send
})

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!