How to edit external JSON file in JavaScript?

I created a small chatbot following the instructions of Esther Crawford . This bot checks the string entered by the user and responds with one of my json answers.

For example, if I say hi, the bot will reply: "Hey, I'm so glad you set up EstherBot!"

script.json

{ "HELLO": "Hey, I'm so glad you set EstherBot up!", "I LOVE YOU": "Awh, shucks! I love you too!", "CONNECT ME": "", "DISCONNECT": "Roger that, EstherBot is back." } 

My question is: how to edit my script.json in JavaScript?

At that moment, when the user enters an unknown string, the bot will reply that he does not understand.

script.js

 if (!_.has(scriptRules, upperText)) { return bot.say('Sorry I dont understand').then(() => 'speak'); } 

How can I get this unknown user string and add it to my script.json file by editing my JSON file in JSON?

I want my bot to learn it myself, if it does not know the answer, it should automatically add the user question to the script.json file, ask the user for an answer, and then add this answer to the script.json file . also.

Thank you very much for your help! You will find this Git project with full code here .

+11
source share
4 answers

You cannot save the file using a client-side script, you must use some server-side scripts, such as PHP, NodeJS, etc., in order to save something to the file.

For example, in NodeJS you can use the fs library:

 fs = require('fs'); var m = JSON.parse(fs.readFileSync('fileName.json').toString()); m.forEach(function(p){ p.name= m.name; }); fs.writeFile('fileName.json', JSON.stringify(m)); 

Hope help

+13
source

Unfortunately, without server-side code — which will accept the request and save the file on the server — it is not possible to save the files.
However, you can use localStorage.

For instance:

 //If statement to check if localStorage already stored. if (!localStorage.script) { localStorage.script = JSON.stringify({ "HELLO": "Hey, I'm so glad you set EstherBot up!", "I LOVE YOU": "Awh, shucks! I love you too!", "CONNECT ME": "", "DISCONNECT": "Roger that, EstherBot is back." }) ; } //This will load the localStorage data into an object in the varaible called botScript var botScript = JSON.parse(localStorage.script) ; function saveScript() { //This will save the current object to the localStorage. localStorage.script = JSON.stringify(botScript) ; } 

You can read more at http://www.w3schools.com/html/html5_webstorage.asp .
You can also use session storage if you want it to be temporary.

+5
source

Assuming you already loaded your JSON:

 var json = '{"hola":"chao"}'; //Parse the JSON: convert it into an object var parsedJson =JSON.parse(json); //add whatever you want parsedJson.hi = 'bye'; 

Your json var will look like this: Object {hola: "chao", hello: "bye"}

You can then convert the object to a string that executes JSON.stringify (parsedJson) and write it back to the / DB disk if you manipulate it in your backend (for example: NodeJs).

+3
source

Javascript (client side), Has no ability to create, edit, etc. files. You can do it inside

0
source

All Articles