Use javascript to update json file

I would like a preface to this, saying that I know what I am doing is not perfect. With this in mind:

I need to update data in a JSON file using javascript. Here is the JSON file that is currently worth:

{ "nextid" : 3, "ingredients" : [ { "id": 1, "name": "onion", "kilojoules": 180, "quantity": 1, "measurement": "whole", "nutrition": [ {"value": "Fat Total", "quantity": 110}, {"value": "Saturated Fat", "quantity": 46}, {"value": "Sodium", "quantity": 4}, {"value": "Carbohydrates Total", "quantity": 10000}, {"value": "Sugar", "quantity": 5000}, {"value": "Fibre", "quantity": 2000}, {"value": "Proten", "quantity": 1000} ]}, { "id" : 2, "name": "carrot", "kilojoules": 56, "quantity": 1, "measurement": "whole", "nutrition": [ {"value": "Fat Total", "quantity": 66}, {"value": "Saturated Fat", "quantity": 11}, {"value": "Sodium", "quantity": 26}, {"value": "Carbohydrates Total", "quantity": 3000}, {"value": "Sugar", "quantity": 2000}, {"value": "Fibre", "quantity": 1000}, {"value": "Proten", "quantity": 279} ]} ] } 

Now let me say that I want to edit the carrot object. I built an updated carrot object on my html page and made it sit like an object in javascript. What will I need to do to update the original json with my changes?

As I said, I know that this is not perfect. I had to use the database to store and process the data, but now I have made my bed, and I need to get this to work, no matter how much it can make you eat everything.

The study tells me that I will need to use PHP or ASP on the server side to collect the parameters that javascript passes to it, but I have no idea where to start.

I work in visual studio 2012, and the project settings forbid me to use add-ons. NuGet code libraries yes, but no addons. Based on this, I think it means that I can not use PHP. Am I thinking about it right?

Note. Yes, this is for the purpose, but modifying the json file is beyond the scope of the requirements. Being able to process json data dynamically is enough for a project, I just REALLY wanted to get it back.

Thank you in advance

EDIT: Probably should have shared this, too. This is javascript that opens a json file. The result of the discovery is stored in a script wide variable called ingJson.

 function downloadIngredients() { $(document).ready(function () { $.getJSON("/data/ingredientsInfo.js", function (result) { try { ingJson = result; ingredients = result.ingredients; ingredients.sort(orderByNameAscending); buildListBox(ingredients); } catch (err) { alert(err.message); } }); }); } 
+6
source share
1 answer

To edit the JSON file:

  • Open file with php
  • Send content to browser (just upload it as a string to a variable in the script tag)
  • Parse JSON with JavaScript ( onload -> get string from script tag)
  • Edit Object
  • Convert it to JSON string again
  • Send back php
  • Have php overwrite the file.

It’s a shame that you can’t just edit the file directly in php, which will make steps 2-6 unnecessary, you just parse , edit and encode JSON in PHP.

Change Since you seem to already have data in JavaScript, steps 1-3 are performed.

+3
source

All Articles