Skip to content
Sign upLog in
← Back to Community

How to use the JSON module in python

Profile icon

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!

import json

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

x = '{"user":"Moonowl24", "favcolor":"pink"}'

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

y = json.loads(x)

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:

import json x = '{"user":"Moonowl24", "favcolor":"pink"}' y = json.loads(x) print(y)

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:

z = json.dumps(y)

Printing it out:


Here's code:

import json x = '{"user":"Moonowl24", "favcolor":"pink"}' y = json.loads(x) print(y) z = json.dumps(y) print(z)

#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!