DEV Community

loading...

Convert json to python

oladejo abdullahi
I am web developer and data scientist
・3 min read

HOW TO CONVERT FROM JSON TO PYTHON

what is json?
JSON is a syntax for storing and exchanging data that is written with JavaScript object notation.
sometimes you might need to fetch data from net or get data from third party.they are usually written in JSON form. JSON data comes just like a string.

How do we deal with json data in python?

well Python has a built-in package called json, which can be used to work with JSON data.
To use the built-in you have to import json then.
If you have a JSON string, you can parse it by using the json.loads() method.
let check the following example

Example1

import json
# some JSON data here:
jsonData = '{ "name":"maxwizard", "age":22,"married":false, "city":"Lagos"}'
# parse json to python:
pythonData= json.loads(jsonData)
# the result is a Python dictionary:
print(pythonData["age"]);
print(pythonData["married"])
Enter fullscreen mode Exit fullscreen mode

OUTPUT

22
False
Enter fullscreen mode Exit fullscreen mode

From the above code jsonData look like a library in python but it is like a string since everything is in single quote. if you print jsonData you will get everything but what you can not print each of the thing in it with keys because it is json data not python. so to convert to python we introduce json.load() to parse the data to python form and we can now access it and use it for what ever we want.

How to manipulate the data

you have to be careful when you want to use the data. although all
json data comes just like a string with javascript notation but it
is very easy to categorize it and know how to deal with them by their looks
these are forms json come and how it look in python
When you convert from Python to JSON, Python objects are converted into the JSON (JavaScript) equivalent:

JSON ----> Python

Object---> dict

Array----> list

Array----> tuple

String---> str

Number--> int

Number--> float

true------> True

false------> False

null-------> None

Let's take a look at more examples:

Example2

import json
jsonCar='["BMW","Ford Edge","Camry","Accord"]' #json inform of Array
Car=json.loads(jsonCar)#for python car in form of list
print(car[0])# call in list method
Enter fullscreen mode Exit fullscreen mode

OUTPUT

BMW
Enter fullscreen mode Exit fullscreen mode

Example3

import json
jsonData='{"x":33,"y":44.55,"name":"maxwizard","Divorce":false}'
python=json.loads(jsonData)
x=python['x']
y=python['y']
name=python['name']
Divorce=python['Divorce']
print(type(python))
print(type(name)) 
print(type(Divorce))
print(type(y))
print(type(x))
Enter fullscreen mode Exit fullscreen mode

OUTPUT

<class 'dict'>
<class 'str'>
<class 'bool'>
<class 'float'>
<class 'int'>
Enter fullscreen mode Exit fullscreen mode

Now let's try a little complex data

Example4

import json
jsonData='[{"name": "John", "age": 30, "city": "New York"},{"name": "maxwizard", "age": 22, "city": "Abuja"},{"name": "Moses", "age": 25, "city": "england"}]'
python=json.loads(jsonData)
print(python[0])
print(python[0]['name'])
print('python[0] type is ',type(python[0]))
print('the python data type is ',type(python))
Enter fullscreen mode Exit fullscreen mode

OUTPUT

{'name': 'John', 'age': 30, 'city': 'New York'}
John
python[0] type is  <class 'dict'>
the python data type is  <class 'list'>
Enter fullscreen mode Exit fullscreen mode

Notice: the json data is an array of object which is convert to list of dictionary in python
Now you are good enough to deal with json data. if you find this helpful consider to follow me! click here to read how to convert python to json!

Discussion (0)

Forem Open with the Forem app