Learn to Code via Tutorials on Repl.it!

← Back to all posts
How to use the JSON module in python
MoonOwl24

Hi and welcome to my latest tutorial, on using the JSON module in python!

What we cover

  • What is the JSON module?
  • Converting into python
    • Printing certain values
  • Converting back into JSON
  • What you can convert
  • Bye for now

What is the JSON module?

The JSON module is a way of converting JSON into python dictionaries or strings and back again. Handy if you want to have JSON data but suddenly you find it would be much easier to handle in python.

Converting into python

First, of course, import!

So, first we get a JSON dictionary and turn it into a variable:

And we use json.loads() to convert the dictionary into python:

Then we print the result:

Printing certain values

If you want to print specific values, it's really easy:

Here is our code so far:

Looking good, isn't it? But we're not quite finished yet...

Converting back into JSON

We use the json.dumps() method to convert from python into JSON. So we get our variable and we convert it back again:

Printing it out:

Here's code:

#What you can convert
Nearly anything!

  • dict
  • list
  • tuple
  • string
  • int
  • float
  • True
  • False
  • None

Bye for now

I attatched a repl below that demonstrates our code. That's all for now!

Voters
MoonOwl24