Python - Pretty Print JSON Array

Submitted by: 
Visitors have accessed this post 263 times.

In this tutorial we will create a Pretty Print JSON Array Using Python. Python is a widely used advance-level programming language for a general technique to the developer. Beginners find Python a clean syntax and indentation structure based, and it is easy to learn because of its less semi colon problem. So let do the coding...

Getting started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python

Importing Modules

After setting up the installation and the database, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python.

Then copy code that I provided below and paste it inside the IDLE text editor.

  1. from tkinter import *

Setting up the Main Frame

After importing the modules, we will now then create the main frame for the application. To do that just copy the code below and paste it inside the IDLE text editor.

  1. #==================================MAIN FRAME=====================================
  2. root = Tk()
  3. root.title("Python - Pretty Print JSON Array")
  4. width=450
  5. height=300
  6. screen_width=root.winfo_screenwidth()
  7. screen_height=root.winfo_screenheight()
  8. x=(screen_width/2) - (width/2)
  9. y=(screen_height/2) - (height/2)
  10. root.geometry("%dx%d+%d+%d" % (width, height, x, y))
  11. root.resizable(0, 0)

Designing the Layout

After creating the Main Frame we will now add some layout to the application. Just kindly copy the code below and paste it inside the IDLE text editor.

  1. #=================================FRAMES==========================================
  2. Top = Frame(root, relief=SOLID, bd=1)
  3. Top.pack(fill=X)
  4. Mid = Frame(root)
  5. Mid.pack()
  6. Bottom = Frame(root)
  7. Bottom.pack(pady=20)
  10. #=================================BUTTON WIDGETS==================================
  11. btn_print = Button(Bottom, text="Print JSON", command=Print)
  12. btn_print.pack()
  14. #=================================LABEL WIDGETS===================================
  15. lbl_title = Label(Top, text="Pretty Print JSON", font=('calibri', 16))
  16. lbl_title.pack()

Creating the Main Function

This is where the code that contains the main functions. This code will get the JSON array and will print on tkinter list. To do that just copy and write these blocks of code inside the text editor.

  1. #=================================METHODS=========================================
  2. def Print():
  3. list = []
  4. for widget in Mid.winfo_children():
  5. widget.destroy()
  7. members = [{
  8. "firstname": "Naruto",
  9. "lastname": "Uzumaki",
  11. }, {
  12. "firstname": "Sasuke",
  13. "lastname": "Uchiha"
  14. }, {
  15. "firstname": "Sakura",
  16. "lastname": "Haruna"
  17. }]
  19. for item in members:
  20. if item not in list:
  21. list.append(item)
  23. for i in range(len(list)):
  24. exec ('Label%d=Label(Mid,text="%s")\nLabel%d.pack(anchor=W)' % (i, list[i], i))

Initializing the Application

After finishing the function save the application as This function will run the code and check if the main is initialize properly. To do that copy the code below and paste it inside the IDLE text editor.

  1. #=================================INITIALIZATION==================================
  2. if __name__ == '__main__':
  3. root.mainloop()

There you have it we just created a Pretty Print JSON Array Using Python. I hope that this simple tutorial help you for what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.