How to edit an external JSON-file in JavaScript?

前端 未结 3 1532
执笔经年
执笔经年 2021-02-02 00:15

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 e

3条回答
  •  无人共我
    2021-02-02 01:15

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

提交回复
热议问题