0

I have a JSON file like so:

{
   key1: "value1",
   key2: "value2",
   key3: {
     key3_1: "value3_1",
     key3_2: "value3_2"
   }
}

I would like to use Node.js to read the JSON file, transform the values for keys where appropriate and write the resulting JSON in a file once the transformation is done. The challenge here is that the transformation of value typically takes a few milliseconds (it is a web request/response flow).

I am unable to think about the correct way of doing it due to the asynchronous nature of Node.js.

Any pointers are highly appreciated.

EDIT
I am not looking for a code example here. Maybe a basic thought process about how it should be done would be fine. I have tried using traverse module. Since it uses this.update to update the nodes, I was not able to make it work with promises (the callbacks wouldn't allow me to access this).

6
  • What have you tried and where did you get stuck? Better questions here research the topic, make some tries at implementing a solution and only post here when you get stuck and need help with a specific problem with the code you wrote, not just asking us to write code for you from scratch with no effort shown on your part at all. Commented Jun 10, 2017 at 16:40
  • You judge too soon! I am not asking for "code"... and mere pointers would have done. I am relatively new to Node.js and I am actually not able to think about the correct way of coding it. I take your comment in a positive spirit. However, I would have appreciated if you would have followed up with a pointer. Commented Jun 11, 2017 at 1:43
  • Did you look at the fs module in nodejs? That's where you read files with nodejs. What did you learn there? Where did you get stuck when you tried read your file with one of those functions? I'm sorry for the directness of my comments, but your question shows no evidence of any effort you put it to research the topic yourself. The traverse module doesn't appear to me to have anything to do with the problem you describe. Read the file, in the completion callback, parse the data, modify the data, convert it back to string, then write it out. That's it. Commented Jun 11, 2017 at 3:57
  • They say a problem well-defined is a problem half solved. I am guilty of not explaining the requirement problem. Apologies! I guess I will break it down and share the sample code that I have been struggling with. Commented Jun 11, 2017 at 5:13
  • If you want help with the this problem in your traverse code, then you have to show your code. It's very solvable, but we would need to see your actual code to know what to suggest. Commented Jun 11, 2017 at 15:58

2 Answers 2

2

To do this asynchronously:

// Import fs module
const fs = require('fs');
// Read the source file.
fs.readFile('source.json', ( err, json ) => {
  // Check for error
  if( err ) {
    console.log( err );
  }
  else {
    // Note that data is a Buffer, convert to string before parsing
    var data = JSON.parse( data.toString() );
    // Modify the data.
    var result = {};
    for( var key in data ) {
      var value = data[key];
      result[key] = modify( value );
    }
    // Serialize result and write to file
    fs.writeFile('result.json', JSON.stringify( result ), ( err ) => {
      if( err ) {
        console.log( err );
      }
      else console.log('Done!');
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

So here is my thought process. Request made -> inside the request call back I would read the file asynchronously -> then modify the data using JSON.parse -> then write the file asynchronously -> then return a response.

The node file api is called fs Good luck!

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.