MegaMix_Craft
MegaMix_Craft

Reputation: 2210

Editing array in json file using javascript

I have a file.json file which contains an array ["number1", "number2"] and I'm trying to add "number3" to that array using javascript. But if I define a variable in js file containing this array and try to edit it, only that variable will be edited, when I'm trying to edit the file itself! It sounds like a simple question, but for some reason I didn't find the answer how to do it. Any help is appreciated!

Upvotes: 1

Views: 147

Answers (1)

dqhendricks
dqhendricks

Reputation: 19251

  1. Get the JSON string from the file.
  2. Turn the JSON string into a javascript object.
  3. Edit the object.
  4. Turn the object back into a JSON string.
  5. Replace the contents of the file with the new JSON string.

Upvotes: 3

Related Questions