1

i need to write the final object formed after a lot of async requests to a JSON file.

Here is the layout of my file

var finalObj = {};
for(i=0;i<100;i++){
  request.get("http://example.com/get/" +i,function(err,res,body){
    var tObj = json.parse(body);
    finalObj[tObj.name] = tObj.value;
  })
}

after all the response object is recieved, i want to save the finalObj to a json file. How do i do this?

3 Answers 3

0

Note that it should be JSON.parse(body) to parse the body correctly.

The easiest thing to do would be to use a counter which gets incremented upon completion, and detect whether you're handled all requests;

var finalObj = {};
var counter = 0;

for(i=0;i<100;i++){
  request.get("http://example.com/get/" +i,function(err,res,body){
    var tObj = JSON.parse(body);
    finalObj[tObj.name] = tObj.value;

    if (++counter === i) {
        require('fs').writeFile('foo.txt', JSON.stringify(finalObj), function (err) { 
            if (!err) {
                // celebrate
            }
        }); 
    }
  });
}

See the File System module for details on the file writing side of things; thankfully they've got a writeFile function which eases the process of writing to a file.

0

That will get data into that object, provided your JSON is correct, but as you said the requests are asynchronous. You will need to add code to monitor when all requests are completed and the object is ready to be written. Then, just write the Javascript object to JSON with JSON.stringify().

2
  • i know how to write a JSON file,,, pls tell me how to be notified when all the requests are completed???
    – shahalpk
    Sep 22, 2012 at 18:56
  • Add a global counter variable and method to write the JSON string. Have each request increment the counter variable (Javascript is not multithreaded, so you won't have race conditions), and have your write method check to see if the count is equal to the number of requests you sent. If it's not, just return. If it is, print the string.
    – monitorjbl
    Sep 22, 2012 at 19:00
0

How about adding a counter to increment each time a request is received? When the counter matches the total number of requests, it's time to write.

var finalObj = {},
    requestCount = 100,
    ticks = 0;

for(i=0; i<requestCount; i++){
  request.get("http://example.com/get/" + i, function (err,res,body) {
    var tObj = json.parse(body);
    finalObj[tObj.name] = tObj.value;
    ticks++;

    // check if all 100 have been received
    if (ticks == requestCount) {
      // write file
    }
  })
}
2
  • bt my situation is lil more worse, im not using a for loop. its a while loop.
    – shahalpk
    Sep 22, 2012 at 19:01
  • is there any generic way to do? or is there any way i can make all these requests sync?
    – shahalpk
    Sep 22, 2012 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.