Skip to content
Sign UpLog In
This post is read-only. Explore Repls and connect with other creators on Community.View Community
The info in this post might be out of date, check out our docs instead. View docs
3

Json loading/saving

ColePayneSexton
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
import json with open
  1. Inside the open add your filename (dont forget the (') or (")!
import json with open('example.txt')
  1. Add what you want to do with the file.
(w) creates a new file (wb) creates a new file in binary (w+) creates a new file but if it exists adds to it (a) adds to the end of an existing file (r) reads a file (defaults to this) (rb) reads a file that was in binary format

Add which letter then as outfile:

import json with open('example.txt, 'w') as outfile:
  1. In the next line you will add json.dump()
import json with open('example.txt', 'w') as outfile: json.dump()

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

import json x = 5 #<<<new variable with open('example.txt', 'w') as outfile: json.dump(x, outfile)

json cant save all data!

json can save the following.

variables (str/float/int) Lists (str/float/int) Dictionaries (str/float/int)

json cannot save

functions (of any kind) classes (of any kind)

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)

import json x = 5 with open('example.txt', 'w') as outfile: json.dump(x, outfile)
  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()
#just loading import json with open('example.txt') as infile:
  1. Instead of json.dump we will use json.load(infile) after a variable name
import json With open('example.txt') as infile: x = json.load(infile)

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

import json #loading x from my other examples try: with open('number.txt') as infile: x = json.load(infile) except FileNotFoundError: print('that file doesn't exist')

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

import json name = input('enter your name') with open('names.txt', 'w+') as outfile: json.dump(name, outfile) with open('names.txt') as infile: name = json.load(infile) Print(name)

Save/load in different scripts

import json x = 5 with open('x.txt', 'w+') as outfile: json.dump(x, outfile)
import json with open('x.txt') as infile: x = json.load(infile) print(x)

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

2 years ago
You are viewing a single comment. View All
1