In the majority of the data mining tasks, you will encounter JSON. Whenever you are going to pull some data from the API’s or other databases, JSON will find you. But you got Python by your side and being said that, convert JSON to string python is no more difficult, or even not at all!!!

json to python strings

Hey, It’s Json!

JSON, JavaScript Object Notation is a light weighted data-interchange format. It is the subset of JavaScript (JS) and deals with the literal object syntax. Anyway, they have some documentation sites to explain the JSON clearly and you can find it here

JSON is popular because it is very easy for humans to work with it and machines can also easily able to parse that. JSON is built on the 2 universal data structures i.e. collection of name/value pairs and ordered list of values.

I hope by now Json is no more a stranger for you, right?

Convert Json to String Python

With python being your side, you can easily convert JSON to string python. You got two methods for this. Let’s illustrate both of them to understand how python can do this job in seconds, literally in seconds!.

json to string python

Method 1:

Whenever you are dealing or working with the API’s the data transfer occurs in the Json format and you need to convert that to strings for the ease of working with that data.

The Method 1 – Is using “Json.dump”.

#Import necessary packages  
import json

#creates a sample json data
test = {'Name':'Hackanons.com','Task':'Convert jason to python string','Method':'One'}
#using json dumps
a = json.dumps(test)

#print the data as string
print(a)
#Checking the type of converted data 
print(type(a))
{"Name": "Hackanons.com", "Task": "Convert json to python string", "Method": "One"}

<class 'str'>

Well, convert Json to string python is much easy than you think. In the above method, we did the conversion using the “Json.dumps” function with ease. You can also check the data type by the ‘type’ function as shown above.

json to python

Method 2:

Welcome to method 2 of converting Json to string python. In this method, we are using an API along with requests and as usual ‘Json.dumps’.

Let’s see how it works.

#Importing the required libraries 
import json
#import requests 
import requests 

#Getting data from API
my_request = requests.get('http://dummy.restapiexample.com/api/v1/employees')
#loading the data 
my_data = json.loads(res.text)

#converting the data to string python
my_data = json.dumps(my_data)

#prints the data
print(my_data)
#prints the data type 
print(type(my_data))
{"status": "success", "data": [{"id": "1", "employee_name": "Tiger Nixon", "employee_salary": "320800", "employee_age": "61", "profile_image": ""}, {"id": "2", "employee_name": "Garrett Winters", "employee_salary": "170750", "employee_age": "63", "profile_image": ""}, {"id": "3", "employee_name": "Ashton Cox", "employee_salary": "86000", "employee_age": "66", "profile_image": ""}, {"id": "4", "employee_name": "Cedric Kelly", "employee_salary": "433060", "employee_age": "22", "profile_image": ""}, {"id": "5", "employee_name": "Airi Satou", "employee_salary": "162700", "employee_age": "33", "profile_image": ""}, {"id": "6", "employee_name": "Brielle Williamson", "employee_salary": "372000", "employee_age": "61", "profile_image": ""}, {"id": "7", "employee_name": "Herrod Chandler", "employee_salary": "137500", "employee_age": "59", "profile_image": ""}, {"id": "8", "employee_name": "Rhona Davidson", "employee_salary": "327900", "employee_age": "55", "profile_image": ""}, {"id": "9", "employee_name": "Colleen Hurst", "employee_salary": "205500", "employee_age": "39", "profile_image": ""}, {"id": "10", "employee_name": "Sonya Frost", "employee_salary": "103600", "employee_age": "23", "profile_image": ""}, {"id": "11", "employee_name": "Jena Gaines", "employee_salary": "90560", "employee_age": "30", "profile_image": ""}, {"id": "12", "employee_name": "Quinn Flynn", "employee_salary": "342000", "employee_age": "22", "profile_image": ""}, {"id": "13", "employee_name": "Charde Marshall", "employee_salary": "470600", "employee_age": "36", "profile_image": ""}, {"id": "14", "employee_name": "Haley Kennedy", "employee_salary": "313500", "employee_age": "43", "profile_image": ""}, {"id": "15", "employee_name": "Tatyana Fitzpatrick", "employee_salary": "385750", "employee_age": "19", "profile_image": ""}, {"id": "16", "employee_name": "Michael Silva", "employee_salary": "198500", "employee_age": "66", "profile_image": ""}, {"id": "17", "employee_name": "Paul Byrd", "employee_salary": "725000", "employee_age": "64", "profile_image": ""}, {"id": "18", "employee_name": "Gloria Little", "employee_salary": "237500", "employee_age": "59", "profile_image": ""}, {"id": "19", "employee_name": "Bradley Greer", "employee_salary": "132000", "employee_age": "41", "profile_image": ""}, {"id": "20", "employee_name": "Dai Rios", "employee_salary": "217500", "employee_age": "35", "profile_image": ""}, {"id": "21", "employee_name": "Jenette Caldwell", "employee_salary": "345000", "employee_age": "30", "profile_image": ""}, {"id": "22", "employee_name": "Yuri Berry", "employee_salary": "675000", "employee_age": "40", "profile_image": ""}, {"id": "23", "employee_name": "Caesar Vance", "employee_salary": "106450", "employee_age": "21", "profile_image": ""}, {"id": "24", "employee_name": "Doris Wilder", "employee_salary": "85600", "employee_age": "23", "profile_image": ""}]}
<class 'str'>

Kudos! You did it. We have Python and we worry about nothing. I have explained 2 methods where you can easily convert the JSON data. These methods are easy, we have created a JSON file in one method, and in another we have requested the data using an API.

python strings

Anyway, try practising with more and more Json data to be familiar with these concepts.

Wrapping Up – Convert Json to string Python

JSON being a subset of JavaScript, it will come into the picture whenever you deal with the data interchanges and data mining and requesting.

This article is comprised of 2 methods that will easily and precisely teach you the way to get the JSON data as a string in Python.

I hope this helps you in getting better on Json’s usage. That’s all for now. Happy Json!!!

More read: Json

Categorized in:

Tagged in: