Skip to content Skip to sidebar Skip to footer

Nodejs - Loop Through Json To Find And Replace The String

In NodeJS I want to find 'This text must change' in json. This is currently my code: var fs = require('fs'), parseString = require('xml2js').parseString, xml2js = require('

Solution 1:

Could you not simply do something like:

fs.readFile('content.xml', 'utf-8', function (err, data){
if(err) console.log(err);

parseString(data, function(err, result){
    if(err) console.log(err);

    var json = result;
    console.log(json);

    var needle = 'This text must change';

    // Convert the result into a string, replace all instances of
    // needle with some text, then parse the json string back into
    // an object
    json = JSON.parse(JSON.stringify(result).replace(needle, SOME_REPLACEMENT_TEXT_HERE));

    ...

});  

Post a Comment for "Nodejs - Loop Through Json To Find And Replace The String"