Dumb question incoming

Ik this seems like a pretty dumb question, but is there a way to make a JSON file and modify it by going to a page? Like code a page to modify the JSON file using a POST request? (like /modify/?value1=hey?value2=there)

Yes!

  1. Build a Node/Express app
  2. Create a JSON file in .data (or anywhere else really)
  3. Make a GET endpoint that retrieves the JSON content (if you need that)
  4. Make a POST /modify/ endpoint that takes parameters like you’ve described above
    • Make it open the JSON file and parse it into an object
    • Modify the object according to the params the user sent through
    • Stringify the object and save it back to your data file

Not a trivial project. You also have to think about how you’re going to map params to JSON keys, as your JSON obj is not necessarily flat.

E.g. This works fine:

{
  "value1": "yo",
  "value2": "wassup"
}
// modify with /modify/?value1=hey?value2=there

But what about this?

{
  "attributes": {
    "speed": 9001,
    "energy" : "high"
  },
  "motd": "be kind, rewind"
}
// how do you modify attributes.energy? 
// Maybe /modify/?attributes.energy=50

That gets tricky to write a handler for.

Hope it helps :peace_symbol:

4 Likes