1

I got a JSON File in which some data is saved. What I want to do is create a function that can be globally used to edit a value. Problem here is that there are different depths and designations of the elements.

Example.json

{ 
  "val": 1,
  "categ": {
    "subcateg": {
      "val1": "#000000",
      "val2": ["123"]
    },
    "subcateg2": {
      "val1": "#000000",
      "val2": ["234"]
    }
  },
  "val1": ["123"],
  "val2": "text stuff",
  "categ2": {
    "id": "123",
    "content": "Hello this is a test"
  }
  .
  .
  .
}

So in the end I want to be able to edit every single value of these.

Current

Until now I just found the solution to do this with eval() but ik it's unsecure so I want a new solution

function json_d_w(keys, value) { // keys: Array, value: any
    json_d_r('', (data) => { // reads the data from the JSON file
        var s = 'data';
        for (var x = 0; x < keys.length; x++) { s += `['${keys[x]}']` }
        eval(s+=' = value');
        return s;
    });
}

So as mentioned before I try to change this to a more secure method but didn't find any in the internet

FileX
  • 773
  • 2
  • 7
  • 19

0 Answers0