Use javascript to update a json file Use javascript to update a json file json json

Use javascript to update a json file


To edit a JSON file:

  1. Open the file with php
  2. Send the contents to the browser (Just dump it as string in a variable in a script tag)
  3. Parse the JSON with JavaScript (onload -> get the string from the script tag)
  4. Edit the object
  5. Convert it to a JSON string again
  6. Send it back to php
  7. Have php overwrite the file.

It's a shame you can't just edit the file directly in php, that'd render steps 2-6 unnecessary, you'd just parse, edit, and encode the JSON in php.

Edit: Since you seem to have the data in JavaScript already, steps 1-3 are accounted for.