Python Tutorial - 14. Working With JSON

codebasics · Beginner ·🛠️ AI Tools & Apps ·10y ago

Key Takeaways

Works with JSON objects in Python using the json module

Full Transcript

hello everyone welcome to core Basics coding tutorials today's topic is working with Json objects in Python and here is the list of items we are covering in this video let's begin with what is Json full form of Json is Javascript object notation it is a data exchange format similar to XML let's understand this by looking at and address record we have this person called Tom and here is his his address book record the way you represent this in Json format is you will have bunch of keys so here name address phone R keys and there are corresponding values which is Tom his address and the phone number the Json object opens and closes with this curly brackets here is the similar representation of the same object in XML format as you can see here in XML the difference is each of these keys are represented as opening and closing tags that's why this takes up lot more volume of data and hence it is not as lightweight as as Json Json is much more lightweight and that's the reason why Json is gaining a lot of popularity nowadays okay now let's work on a problem of creating an address book so we what we're going to do is we'll create an address book we'll write bunch of Records into it and then we will save it as a file on our computer then we will write a second program to read that address book and print those address book records okay so first I'm going to create a dictionary object there is no object called a Json in Python Python's native objects are either numbers strings dictionaries Etc Json is just a concept Json is basically a a format which is implemented by different languages so all C++ JavaScript python all languages supports it but otherwise it's it's a very generic concept versus dictionary is just specific to python okay here in this dictionary we will add a first track cord for Tom and we'll say his name is Tom his address is this and the phone number is this okay let's create another object another record rather in our book dictionary and we are going to call it Bob and Bob's address is this okay let's type in some random phone number okay now what we will do is we will import a Json module so that is the standard module available in Python called Json and once imported you can call this method called dump s what this is doing is it is taking dictionary object which is book as an input then it is dumping it as a string that's why we have a string here and when it converts this dictionary object into a string it will convert it into a Json format that's why we have we are using Json module here so when you say s equal to this the string s will be in Json format let's first print it and see how it looks when you say run address as you can see here in the output this thing is a big string but it is in a Json format it has all opening closing brackets columns to separate keys and values and so on okay now we will write this to a file so I'll just remove this and I'll say with open C data book. dxt we are going to save this address book in C datab book. txt file and if you remember from my file tutorial if you want to write to a file you have to use W here you will just say f. write s so this will write this string Json string to a file let let's execute this excellent it says program finished with exit Code Zero means there were no errors now let's go and check our C data directory so I have C data directory here and I'm seeing book. txt file if I open it I see the content that I wrote to that file this content is in a gson format the advantage now is that once you have content into a gs1 format format you can write a C++ program or let's say JavaScript program to open this and read the the the those objects successfully okay that's why it's called Json is a data exchange format so you can exchange data from your Python program to C program okay but here to read that we are going to use another Python program uh and for that I will use idle so let me bring my idle here here and using idle I will write another program that reads those records one by one okay so again you have to use uh you have to first open the file and this time I will use R because I'm reading it for writing you use W okay you will say s equal to f. read this is going to read the whole file into this variable called s if you print s it looks like this you have entire files contain into this variable now if you want to leten know Bob's phone number you have to use a Json module to read this Json string and pass it into a Json object okay so I will import Json module here and I will use another matter called Json do load string that's why it is load as load string s okay and once it loads it what it will do is it will convert that string into our dictionary so I will say book okay if I print book here the difference between this output and the next output is this guy was a Str Str versus this guy here is a dictionary you can confirm it using type function type of book is a dick all right how do I know Bob's phone number first let's print Bob's entire address book record when you do this it is printing the whole address book record for Bob okay if you want to now know the phone number you will say Bob and phone okay this is printing his phone number once you convert it to a dictionary object it is very easy to access the information within that big object using the keys and that that's a power of Json and dictionaries all right next thing we are going to do is now we are going to print all the address book records in our book for that you have to use for Loop you will say for person in book print book person this is going through all of the keys in the dictionary 1 by one and we are printing the corresponding values excellent so it's printed all these records one by one you can have like hundreds of records and it will still work fine okay so that was all about Json object we covered how to read and how to write Json objects and how to convert them to dictionary back and forth okay thanks for watching

Original Description

In this python tutorial, we are going to see working with “JSON objects” in python. The topics that we have covered in this video are how to work with “JSON format” in python and how to “import JSON module”. Topics that are covered in this Python Video: 0:14 What is JSON 3:32 Import json module 3:45 dumps() method Video without background music: https://youtu.be/FNExLliAnZw Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses. Learn how to deal with JSON objects in python. JSON (Javascript Object Notation) is a data exchange format (like XML) but it is much light weight. Next Video: Python Tutorial - 15. if __name__ == "__main__": https://www.youtube.com/watch?v=Huz6bS0uLm4&list=PLeo1K3hjS3usILfyvQlvUBokXkHPSve6S&index=17 Visit my website http://codebasicshub.com/ for complete list of programming tutorials. Link for code: https://github.com/codebasics/py/blob/master/Basics/14_json_addressbook.py Website: https://codebasics.io/ Facebook: https://www.facebook.com/codebasicshub Twitter: https://twitter.com/codebasicshub
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from codebasics · codebasics · 15 of 60

1 Python Tutorial - 1. Install python on windows
Python Tutorial - 1. Install python on windows
codebasics
2 Python Tutorial - 2. Variables
Python Tutorial - 2. Variables
codebasics
3 Python Tutorial - 3. Numbers
Python Tutorial - 3. Numbers
codebasics
4 Python Tutorial - 4. Strings
Python Tutorial - 4. Strings
codebasics
5 Python Tutorial - 5. Lists
Python Tutorial - 5. Lists
codebasics
6 Python Tutorial - 6. Install PyCharm on Windows
Python Tutorial - 6. Install PyCharm on Windows
codebasics
7 PyCharm Tutorial - 7. Debug python code using PyCharm
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
8 Python Tutorial -  8. If Statement
Python Tutorial - 8. If Statement
codebasics
9 Python Tutorial - 9. For loop
Python Tutorial - 9. For loop
codebasics
10 Python Tutorial -  10. Functions
Python Tutorial - 10. Functions
codebasics
11 Python Tutorial - 11. Dictionaries and Tuples
Python Tutorial - 11. Dictionaries and Tuples
codebasics
12 Python Tutorial - 12. Modules
Python Tutorial - 12. Modules
codebasics
13 Python Tutorial - 13. Reading/Writing Files
Python Tutorial - 13. Reading/Writing Files
codebasics
14 How to install Julia on Windows
How to install Julia on Windows
codebasics
Python Tutorial - 14. Working With JSON
Python Tutorial - 14. Working With JSON
codebasics
16 Julia Tutorial - 1. Variables
Julia Tutorial - 1. Variables
codebasics
17 Julia Tutorial - 2. Numbers
Julia Tutorial - 2. Numbers
codebasics
18 Python Tutorial - 15. if __name__ == "__main__"
Python Tutorial - 15. if __name__ == "__main__"
codebasics
19 Julia Tutorial - Why Should I Learn Julia Programming Language
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
20 Python Tutorial  - 16. Exception Handling
Python Tutorial - 16. Exception Handling
codebasics
21 Julia Tutorial - 3. Complex and Rational Numbers
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
22 Julia Tutorial - 4. Strings
Julia Tutorial - 4. Strings
codebasics
23 Python Tutorial -  17. Class and Objects
Python Tutorial - 17. Class and Objects
codebasics
24 Julia Tutorial - 5. Functions
Julia Tutorial - 5. Functions
codebasics
25 Julia Tutorial - 6. If Statement and Ternary Operator
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
26 Julia Tutorial - 7. For While Loop
Julia Tutorial - 7. For While Loop
codebasics
27 Python Tutorial  - 18. Inheritance
Python Tutorial - 18. Inheritance
codebasics
28 Julia Tutorial - 8. begin and (;) Compound Expressions
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
29 Python Tutorial - 12.1 - Install Python Module (using pip)
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
30 Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
31 Julia Tutorial - 10. Exception Handling
Julia Tutorial - 10. Exception Handling
codebasics
32 Python Tutorial  - 19. Multiple Inheritance
Python Tutorial - 19. Multiple Inheritance
codebasics
33 Python Tutorial - 20. Raise Exception And Finally
Python Tutorial - 20. Raise Exception And Finally
codebasics
34 Python Tutorial - 21. Iterators
Python Tutorial - 21. Iterators
codebasics
35 Python Tutorial - 22. Generators
Python Tutorial - 22. Generators
codebasics
36 Python Tutorial - 23. List Set Dict Comprehensions
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
37 Python Tutorial - 24. Sets and Frozen Sets
Python Tutorial - 24. Sets and Frozen Sets
codebasics
38 Python Tutorial - 25. Command line argument processing using argparse
Python Tutorial - 25. Command line argument processing using argparse
codebasics
39 Debugging Tips - What is bug and debugging?
Debugging Tips - What is bug and debugging?
codebasics
40 Debugging Tips - Conditional Breakpoint
Debugging Tips - Conditional Breakpoint
codebasics
41 Debugging Tips - Watches and Call Stack
Debugging Tips - Watches and Call Stack
codebasics
42 Python Tutorial - 26. Multithreading - Introduction
Python Tutorial - 26. Multithreading - Introduction
codebasics
43 Git Tutorial 3:  How To Install Git
Git Tutorial 3: How To Install Git
codebasics
44 Git Tutorial 1: What is git / What is version control system?
Git Tutorial 1: What is git / What is version control system?
codebasics
45 Git Tutorial 2 : What is Github? | github tutorial
Git Tutorial 2 : What is Github? | github tutorial
codebasics
46 Git Tutorial 4: Basic Commands: add, commit, push
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
47 Git Tutorial 5: Undoing/Reverting/Resetting code changes
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
48 Git Tutorial 6: Branches (Create, Merge, Delete a branch)
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
49 Git Github Tutorial 10: What is Pull Request?
Git Github Tutorial 10: What is Pull Request?
codebasics
50 Git Tutorial 7: What is HEAD?
Git Tutorial 7: What is HEAD?
codebasics
51 Git Tutorial 9: Diff and Merge using meld
Git Tutorial 9: Diff and Merge using meld
codebasics
52 Difference between Multiprocessing and Multithreading
Difference between Multiprocessing and Multithreading
codebasics
53 Python Tutorial - 27. Multiprocessing Introduction
Python Tutorial - 27. Multiprocessing Introduction
codebasics
54 Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
55 Git Tutorial 8 - .gitignore file
Git Tutorial 8 - .gitignore file
codebasics
56 Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
57 Python Tutorial - 30. Multiprocessing Lock
Python Tutorial - 30. Multiprocessing Lock
codebasics
58 Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
59 What is code?
What is code?
codebasics
60 Python unit testing - pytest introduction
Python unit testing - pytest introduction
codebasics

Related Reads

📰
How I Built a Free Online Image & PDF Processing Platform with Vue 3 + FastAPI
Learn how to build a free online image and PDF processing platform using Vue 3 and FastAPI, and discover the benefits of combining these technologies for efficient file processing
Dev.to · IAMUU
📰
I Built a Free AI-Powered YouTube SEO Toolkit With Zero Budget. Here’s What Actually Happened.
Learn how a solo dev built a free AI-powered YouTube SEO toolkit with zero budget and the lessons they learned from the experience
Medium · Startup
📰
How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Learn to create a second version of yourself inside Obsidian using AI with a step-by-step guide
Medium · ChatGPT
📰
How to prepare for Spain civil service TIC exam using AI in 2026
Learn how to prepare for the Spain civil service TIC exam using AI in 2026, boosting your chances of success with technology-driven study techniques
Dev.to · David García

Chapters (3)

0:14 What is JSON
3:32 Import json module
3:45 dumps() method
Up next
I Asked Gemini to Build a Dashboard... I Didn't Expect This
Patech
Watch →