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).
fsmodule 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.thisproblem 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.