Python Tutorial - 14. Working With JSON
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
2
3
4
5
6
7
8
9
10
11
12
13
14
▶
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Python Tutorial - 1. Install python on windows
codebasics
Python Tutorial - 2. Variables
codebasics
Python Tutorial - 3. Numbers
codebasics
Python Tutorial - 4. Strings
codebasics
Python Tutorial - 5. Lists
codebasics
Python Tutorial - 6. Install PyCharm on Windows
codebasics
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
Python Tutorial - 8. If Statement
codebasics
Python Tutorial - 9. For loop
codebasics
Python Tutorial - 10. Functions
codebasics
Python Tutorial - 11. Dictionaries and Tuples
codebasics
Python Tutorial - 12. Modules
codebasics
Python Tutorial - 13. Reading/Writing Files
codebasics
How to install Julia on Windows
codebasics
Python Tutorial - 14. Working With JSON
codebasics
Julia Tutorial - 1. Variables
codebasics
Julia Tutorial - 2. Numbers
codebasics
Python Tutorial - 15. if __name__ == "__main__"
codebasics
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
Python Tutorial - 16. Exception Handling
codebasics
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
Julia Tutorial - 4. Strings
codebasics
Python Tutorial - 17. Class and Objects
codebasics
Julia Tutorial - 5. Functions
codebasics
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
Julia Tutorial - 7. For While Loop
codebasics
Python Tutorial - 18. Inheritance
codebasics
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
Julia Tutorial - 10. Exception Handling
codebasics
Python Tutorial - 19. Multiple Inheritance
codebasics
Python Tutorial - 20. Raise Exception And Finally
codebasics
Python Tutorial - 21. Iterators
codebasics
Python Tutorial - 22. Generators
codebasics
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
Python Tutorial - 24. Sets and Frozen Sets
codebasics
Python Tutorial - 25. Command line argument processing using argparse
codebasics
Debugging Tips - What is bug and debugging?
codebasics
Debugging Tips - Conditional Breakpoint
codebasics
Debugging Tips - Watches and Call Stack
codebasics
Python Tutorial - 26. Multithreading - Introduction
codebasics
Git Tutorial 3: How To Install Git
codebasics
Git Tutorial 1: What is git / What is version control system?
codebasics
Git Tutorial 2 : What is Github? | github tutorial
codebasics
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
Git Github Tutorial 10: What is Pull Request?
codebasics
Git Tutorial 7: What is HEAD?
codebasics
Git Tutorial 9: Diff and Merge using meld
codebasics
Difference between Multiprocessing and Multithreading
codebasics
Python Tutorial - 27. Multiprocessing Introduction
codebasics
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
Git Tutorial 8 - .gitignore file
codebasics
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
Python Tutorial - 30. Multiprocessing Lock
codebasics
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
What is code?
codebasics
Python unit testing - pytest introduction
codebasics
Related Reads
📰
📰
📰
📰
How I Built a Free Online Image & PDF Processing Platform with Vue 3 + FastAPI
Dev.to · IAMUU
I Built a Free AI-Powered YouTube SEO Toolkit With Zero Budget. Here’s What Actually Happened.
Medium · Startup
How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Dev.to · David García
Chapters (3)
0:14
What is JSON
3:32
Import json module
3:45
dumps() method
🎓
Tutor Explanation
DeepCamp AI