node.jsjsondiscord.jsfile-access

Node Js Acessing a Jason File


still new to JSON and while ive searched the net and created a function to create a init file if none exists i'm coming up blank for search and retrive the data of the new existing file or how I add new entries or update new entries

so far i can do a read file and export the resits in a console log so i know the assignment work, its a global variable so the data should persist out of the read file loop but when i try and access it later to make the local array i'll pull data from and use for updating later it reads as undefined.

fs.readFile(path, 'utf8', (error, data) => {
    if(error){
      console.log(error);
      return;
    }
    //console.log(JSON.parse(data));
    JSONData = JSON.parse(data);
    for (let i = 0; i < JSONData.length; i++) {
      console.log(i+": ["+JSONData[i].unique+"] "+JSONData[i].name);
    }
});//fs.readFile
  var playerKey = "KuroTO";
    playerKey = playerKey.toLowerCase();
    
    for (let i = 0; i < JSONData.length; i++) {
      if (JSONData[i].unique.toLowerCase() == playerKey){
        console.log("["+i+"] "+JSONData[i].unique.toLowerCase()+": "+playerKey);
        PlayerCard1.push(JSONData[i].userid);//0
        PlayerCard1.push(JSONData[i].username);//1
        PlayerCard1.push(JSONData[i].unique);//2
        PlayerCard1.push(JSONData[i].name);//3
        PlayerCard1.push(JSONData[i].avatarurl);//4
        PlayerCard1.push(JSONData[i].level);//5
        PlayerCard1.push(JSONData[i].Rank);//6
        PlayerCard1.push(JSONData[i].henshined);//7
        PlayerCard1.push(JSONData[i].Strength);//8
        PlayerCard1.push(JSONData[i].Perception);//9
        PlayerCard1.push(JSONData[i].Endurance);//10
        PlayerCard1.push(JSONData[i].Wisdom);//11
        PlayerCard1.push(JSONData[i].Intelligence)//12;
        PlayerCard1.push(JSONData[i].Luck)//13;
        PlayerCard1.push(JSONData[i].Agility)//14;
        PlayerCard1.push(JSONData[i].Flexability)//15;
        PlayerCard1.push(JSONData[i].RatedSpeed)//16;
      };//if unique matches
    };//for
    

this is ther psudo code concept im trying to do if (JSONData.stringify.unique == {SearchUID}){toonname = JSONData.stringify.name;}

as i understand it you cant really apend just rewrite the file over again with new data and i think i can figure that out on my own once i cand figure out how to real the file into an array i can search like above


Solution

  • To read JSON, simply require the file.

    JSON:

    {
        "key": "H"
    }
    

    JS:

    let jsonFile = require("./path/to/json");
    console.log(jsonFile.key); // H
    

    Editing is just as simple.

    let jsonFile = require("./path/to/json");
    jsonFile.key = "A" 
    console.log(jsonFile.key) // A
    

    Saving edits requires use of FileSystem:

    const fs = require("fs")
    let jsonFile = require("./path/to/json");
    jsonFile.key = "A" 
    // first argument is the file path
    // second argument is the JSON to write - the file is overwritten already 
    // due to above, so just JSON.stringify() the required file.
    // third argument is an error callback
    fs.writeFile("./path/to/jsonFile", JSON.stringify(jsonFile), (err) => {
        if (err) throw new Error(err);
    });
    

    This can also be used to slightly clean up your current init function if you wanted, but that's up to you of course.