Forums

Writing back to json?

Hi guys,

I have a table which is populate via json file in /static. The user can then interact with the table changing some boolean values.

I would like to save the changes by overwriting the original json.

Aside from the conversion from table th tr td down to json in javascript what is the best method of the actual writing this?

I know using node js is not really an option on python anywhere and i'm a little unsure on best practice using python in html page too. Thanks for any help! =)

I realize similar questions have been asked before but I can't make sense of them :(

I assume the best method is using:

 f = open("my_json_file.json", "w+")
 f.write(json_object)
 f.close()

I'm just not sure how to add python code like that inside a javascript function inside a .html document....

There are ways to run Python in a browser, but in general that is not necessary. You would need to write stuff for the browser in Javascript. In your case, you can do it in 2 ways:

  1. Put the table into an HTML form and POST it to the server. The server could then take the POST data (whcih would not be in JSON) and convert it to JSON and save it.
  2. Write Javascript to look at your table and create the JSON and then POST that to the server whcih could then just save it.

Yep! Thanks glenn figured it out!

Was simple in the end. Is there any benefit of storing the JSON file in Postgres as oppose to a static file? (Still very new at web development just wanting to know what to explore more next).

:)

A normal setup for a website wouldn't be writing JSON to the filesystem or the database at all -- instead, it would use JSON as a format for talking to JavaScript, but then store the database in tables in a regular SQL database. But if JSON format suits your data better than the more structured table-based SQL style, you can certainly store it in Postgres or MySQL. If it's for your first app, I'd definitely look into using MySQL -- then the more complex JSON-based storage would come later, once you've got more experience.

I would definitely recommend that you use a database, though, regardless if the data is in tables or JSON. The reason is that when you have lots of people using your site, they might try to access the same data concurrently. Databases have locking code that stops people from trampling over each other's changes, whereas if you're writing stuff to a file, two concurrent writes might break it.

Подскажите как написать сам jsone на дежурного ирисса

The JSON you need to write will depend on what you are using it for. The documentation of whatever is using the JSON should indicate the format that you need to use.

Hello,

I have a related JSON question. I am storing info from my webapp as JSON files via flask in the static folder. I plan to include a database later. For now the webapp overwrites the file everytime there is a change. On my localhost it works fine, but the same setup doesn't work in Pythonanywhere. (An earlier version of the webapp, where I wasn't writing files, was working fine.) It seems like the 'original' json files are locked, because javascript makes the changes then sends it to flask to do other commands and save the info as a json file. When javascript reloads the json files, it quickly goes back to the settings without the change. What could the issue be? Thanks in advance for any leads.

When do you open and close that file? Do you do it in a view or outside of it? Take look at our beginner's Flask tutorial, it introduces the database gently https://blog.pythonanywhere.com/121/

Thank-you for your quick response and for your reference to the tutorial. Somehow the problem resolved itself a few hours later when I was running some tests. Thanks again.