0

I have a JSON file in my pc "WordMeanings.json" and the file has the data

{
  "WordMeanings": [
      {
          "bangla": "ei je",
          "example": "Hi there!",
          "english": "Hi"

      }
]
}

but I would like to append new JSON object data to existing JSON file something like the below objects by html input form

,{
          "bangla": "ki obostha?",
          "example": "What's up?"
          "english": "How are you?"

      }

The html form will be 4 inputs Bangla, Example, English and Submit button. When I will click on the Submit button the inputs will add on the existing JSON file under the existing objects in "WordMeanings.json" and it should be adding how many times i want to add.

I have created the input form and try to do it by html and javascript but i couldn't!

  • Unfortunately you can't easily do that and have valid JSON. You're going to have to read and parse the JSON. Make any changes you want and then re-stringify and write to the file. – phuzi Dec 08 '22 at 12:54
  • You can't with javascript only, `Javascript` is a client-side language, it has no access to make write operations on the OS, you also need a server-side language to do that for example `Nodejs`. – Mina Dec 08 '22 at 12:55

1 Answers1

0

Assuming that WordMeanings.json is web accessible URL:

  1. Read JSON from URL. E.g. $.getJSON('WordMeanings.json', successCallback)
  2. In successCallback append element to JSON Object:
function successCallback(data) {
    data.WordMeanings.push({
          "bangla": "ki obostha?",
          "example": "What's up?"
          "english": "How are you?"
     });
}
  1. Do whatever you want with new data. E.g. let user download it
Justinas
  • 41,402
  • 5
  • 66
  • 96
  • I am trying by the codes. The new array showing on the console log but nothing push in the existing JSON file. – Najmul Hasan Dec 11 '22 at 04:30
  • @NajmulHasan Well, how are you saving that new JSON array into file? It will not magically appear in file unless you do something with it – Justinas Dec 12 '22 at 07:33