Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit an external JSON-file in JavaScript?

I have created a little chat bot following the tutorial of Esther Crawford. This bot check the string that enter the user and respond with one of my json answer.

For example if I say "hello" the bot 'll respond "Hey, I'm so glad you set EstherBot up!"

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?

For the moment when the user enters an unknown string, the bot will answer that it doesn't understand.

script.js

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

How could I get this unknown string of the user and add it in my script.json file by editing in JavaScript my JSON-file ?

I want that my bot learn by itself, if he doesn't know the answer it should automatically add the question of the user to the script.json file, ask the user for an answer and then add this answer in the script.json file too.

Many thanks for your help! You'll find this project on git with the full code here.

like image 386
Shark Avatar asked Apr 26 '16 15:04

Shark


People also ask

How do I edit an existing JSON file?

In the Enterprise Explorer view, right-click your . json file or other file type that contains JSON code and select Open With > JSON Editor. You can compress JSON strings so that the strings display on one line with white space removed between JSON elements.

Can you write to a JSON file in JavaScript?

JavaScript provides a built-in JSON object for parsing and serializing JSON data. You can use the JSON. stringify() method to convert your JSON object into its string representation, and then use the file system fs module to write it to a file.


3 Answers

You can't save in a file using client-side script, you have to use some server-side scripting like PHP, NodeJS etc. to save something in a file.

For example in NodeJS you can use the fs library:

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

Hope it helps

like image 78
Vaibhav Jain Avatar answered Sep 29 '22 08:09

Vaibhav Jain


Unfortunately, without having server-side code - that would take a request & store the file on the server - it is not posible to save files.
However you could use localStorage.

For example:

//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 could also use session storage if you want it to be temporary.

like image 22
Jacob Avatar answered Sep 29 '22 08:09

Jacob


Assuming you got your json already loaded:

var json = '{"hola":"ciao"}';

//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:

Object {hola: "ciao", hi: "bye"}

Then you can convert the object to string doing JSON.stringify(parsedJson) and write back to disk/DB if you're manipulating it in your backend (ie.: NodeJs).

like image 20
fapm84 Avatar answered Sep 29 '22 09:09

fapm84