Learn to Code via Tutorials on Repl.it!

← Back to all posts
Json loading/saving
ColePayneSexton

what is json?

jason is a module for python that allows a user to 'sterilize' data to use later or even in entirely different scripts.

why json?

Im going to be honest there are better options out there but json is very simple compared to most of the other ways to save data

key points

1) how to get json
2) how to save data
3) how to load data
4) handling errors
5) quick review and full example

how to get json

To get json all you need to do (on repl.it) is import json
Sometimes on other IDEs you need to install it by going to the terminal and entering pip install json

how to save data

Saving data with json is fairly simple.
First you need to make sure you have it imported/installed
1. Use the function with and open to open a file, im just going to use the name 'example.txt` to show where to put the example

  1. Inside the open add your filename (dont forget the (') or (")!
  1. Add what you want to do with the file.

Add which letter then as outfile:

  1. In the next line you will add json.dump()

4.5. Inside the dump() add the item you want to save and outfile

json cant save all data!

json can save the following.

json cannot save

You have completed the 'how to save data' portion

loading data

So now you have your stuff saved, you want to use it again

(So far)

  1. We are going to use the same thing up to the start of 'outfile' and replace it with infile and completely leave behind the 'w' in the open()
  1. Instead of json.dump we will use json.load(infile) after a variable name

handling errors

what errors to expect

The most common errors i get is FileNotFoundError

FileNotFoundError is an error you get if you try to load a file that doesn't exist
This is common as a typo.

To pervent yourself from getting this error you have to learn how to handle errors, this can be done with try and except

try will try to do anything that follows (before the next indent block or except)
except will wait for a given error and if it receives it it does whats after it (before next indent block)

These two follow the syntax of try before except

output: that file doesn't exist
Why?
Because if we are loading x from the other examples it is saved to the file example.txt not number.txt meaning the file number.txt doesn't exist causing a FileNotFoundError but because FileNotFoundError was put after the except it didnt stop the program.

reveiw

import json adds json to your script
with open() opens the file name
json.dump saves data
json.load loads in saved data
try/else pervents an error

Full example
Loading into the same script

Save/load in different scripts

Extra note
If there are spelling miscakes or inaccuracies let me know, i made this in a rush.

Voters
lolguineapig
ColePayneSexton
Comments
hotnewtop
lolguineapig

There are lots of spelling mistakes such as how you wrote "jason" at the beginning and it says "miscakes" at the end.

lolguineapig

But this tutorial was helpful.