Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use javascript to update a json file

I'd like to preface this by saying I know what I'm doing isn't ideal. With that covered:

I need to update data in a JSON file via javascript. Here is the JSON file as it currently stands:

{
"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's say I want to edit the carrot object. I've built an updated carrot object on my html page and have it sitting as an object in the javascript. What am I going to need to do to update the source json with my edits?

Like I said before, I know this isn't ideal. I should have used a database for storing and manipulating data, but I've made my bed now and I have to get this working, regardless of how much it might make you all cringe.

Research tells me I'm going to need to use PHP or ASP on the server side to collect the parameters the javascript passes to it, but I have no idea where to begin with that.

I'm working in visual studio 2012 and the parameters of the project prohibit me from using addons. NuGet code libraries yes, but no addons. On that basis, I think it means I can't use PHP. Am I correct in my thinking there?

Note: Yes it's for an assignment, but altering the json file is beyond the scope of the requirements. Being able to process json data dynamically is enough for the project, I'd just REALLY like to be able to put some back.

Thanks in advance

EDIT: Probably should have shared this too. This is the javascript that opens the json file. The result of the opening 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);
        }
    });
});
}
like image 342
DrBoon Avatar asked Jan 31 '14 10:01

DrBoon


1 Answers

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.

like image 170
Cerbrus Avatar answered Sep 21 '22 07:09

Cerbrus