0

Looking to adjust a config file to run a program, and I got a few lines that makes the script running. But I need to add more units, therfore add 500 files with same config information in the .json but edit two lines from each file.

How would I easiest load the .json files in node.js, remove the two lines in the config and write the new lines in each easiest?

Thanks a lot for any assistance

I need to adjust the "secretCode" in each file to a new one, i.e

{
    "name": "James",
    "birthday": "March 22",
    "secretCode": 16309
}

{
    "name": "James",
    "birthday": "March 22",
    "secretCode": 15220
}
Cellcon
  • 1,245
  • 2
  • 11
  • 27
  • Please show us, what you have tried so far. – Cellcon Dec 12 '17 at 21:02
  • Did I get this right, you want to remove 2 lines from 500 json files each and merge the result into one big json file? – Cellcon Dec 12 '17 at 21:04
  • Well I'm a very inexperienced coder so my only solution was to manually edit these files, but as I might expand this project I'll add 500 more-- so figured I might aswell try learn how to easily find a better solution. I need 500 different config files, each file should have same config except 2 lines which should be different (to determine the account) – John Davis Dec 12 '17 at 21:04
  • So, you just want to know how to remove lines from a file in node.js? How do you identify these lines? Is it e.g. always the third and fourth line? – Cellcon Dec 12 '17 at 21:05
  • It's always the same line, I just need to post a different username and password for each file. – John Davis Dec 12 '17 at 21:07
  • If I could help you out and answered your question, I would be very happy, if you would accept my answer. – Cellcon Dec 14 '17 at 08:41

1 Answers1

0

Below you will find how to replace a line in a text file for reference and a good one, too. But you really should not use this method to update a JSON file. What you should want, is parse the JSON data, so you can edit the data and save the edited data in a text file.

So, we are not speaking about lines here, but about data, that has been serialized into the JSON format. The stored data must be updated.

There are a lot of problems, which editing a JSON files line by line could cause. In the end, the file could get corrupted and get your system into an undesirable state.

Both of the implementations below are synchronous, as this might be easier to understand for a beginner.

Please remember to always create a backup, when working with production data.


How to do it

This script will parse a json file, change the secretCode and save it back into the same file.

var fs = require('fs');

function replaceDataInJsonFile(filePath, propertyName, newData) {
    // Read the complete file content into a tring.
    var fileText = fs.readFileSync(filePath, 'utf-8');
    // Create an object containing the data from the file.
    var fileData = JSON.parse(fileText);

    // Replace the secretCode in the data object.
    fileData[propertyName] = newData;

    // Create JSON, containing the date from the data object.
    fileText = JSON.stringify(fileData, null, "    ");
    // Write the updte file content to the disk.
    fs.writeFileSync(filePath, fileText, 'utf-8');
}

// Call the function above.
replaceDataInJsonFile('test.json', 'secretCode', 2233);

How not to do it

This script will overwrite the fourth line of the utf-8 encoded file test.json. The test.json file is located in the execution directory of Node.js.

var fs = require('fs');

function replaceLineInFile(filePath, lineIndex, newLineText) {
    // Read the complete file content into a tring.
    var fileText = fs.readFileSync(filePath, 'utf-8');

    // Get line break chars.
    var lineBreakChars = '\r\n';
    if (fileText.indexOf(lineBreakChars) > -1) {
        lineBreakChars = '\n';
    }

    // Split the file content into an array of lines.
    var fileLines = fileText.split(lineBreakChars);

    // Overwrite a line from the array of lines.
    fileLines[lineIndex] = newLineText;

    // Join the line array elements into a single string again.
    fileText = fileLines.join(lineBreakChars);
    // Write the updte file content to the disk.
    fs.writeFileSync(filePath, fileText, 'utf-8');
}

// Call the function above.
replaceLineInFile('test.json', 3, '    "secretCode": 2233');

Input/Output File

Both scripts may take the following lines in an input file, update the data and write the updated data back into the file.

The content of test.json, before the script has been executed.

{
    "name": "James",
    "birthday": "March 22",
    "secretCode": 16309
}

The content of test.json, after executing node myscript.js, with myscript.js containing the code from above.

{
    "name": "James",
    "birthday": "March 22",
    "secretCode": 2233
}
Cellcon
  • 1,245
  • 2
  • 11
  • 27
  • Alright I think I'm getting somewhere but I have to leave for now. Will get back tomorrow. Thanks a lot for the help, I got the code in a text document with 500 different one's just not sure how to add in those in the correct line. – John Davis Dec 12 '17 at 22:29
  • If you want to edit 500 files in a directory, please have a look at this question: https://stackoverflow.com/questions/2727167/ Just do not list the filenames, but use them as arguments for the `replaceDataInJsonFile` function above. – Cellcon Dec 12 '17 at 22:34