How To Use JSON In Python
Key Takeaways
The video demonstrates how to work with JSON data in Python, covering topics such as loading and creating JSON strings, loading and dumping JSON data from and to files, and using the json library to handle JSON data.
Full Transcript
[Music] in this video i'm going to show you how to work with json data in python as fast as possible so to get started json stands for javascript object notation now this is a standard language javascript is really not an important part of the name although it was originally based on javascript now the syntax for json is very similar to that of a python dictionary you can see this a valid json string right here we start with our curly braces we have a key that key is associated with some type of value in this case it's an array or a list then inside of here i have other json objects i have keys i have values i can represent numbers strings etc now one important note in json is you have to use double quotation marks not single quotation marks like you can use in python now json data is commonly used for configuration files storing some type of data or commonly for api so if you ever worked with an api before you've probably received data back in json and had to send data as json as well so let's see how we can take a string that is representing json data and actually load that in as a json object or a python dictionary because look looks very similar to a python dictionary that means we're probably going to use a python dictionary to represent it so to do this is very simple what i can do is type data is equal to json.load s now i'm doing s because we're loading a string you can also load a json file which i will show you in a second and then here i'm going to put json string now if i print out my data you're going to see that i get all of the data here except it's actually in a python dictionary so if i go ahead and print out data at and let's just do the key students now you're going to see that this works i can get all of my students i could also access say the first index here and you'll see that i get that individual stu now since i forgot to mention this i did import the json library to be able to do this this is built into the standard library of python this means you don't need to install it it's right here you can just import it and then use it like this anyways that's as easy as it is to actually load data in using the json library we have a json string we use json.load make sure we have the s at the end put the string it gives us a python dictionary now if we have an error here in our json string this will give us an error as well so just keep that in mind if you're trying to load a string that's not valid json then it's not going to work you're going to get an error so in the same way that we can load a string that is json data we can dump a python dictionary into json formats let me show you how we do that now we have a dictionary already it's the data dictionary let's just add a key to it so let's say data at test is equal to true okay just a random key here now if i want to dump this into a json string i can do the following so i'll just say new underscore json is equal to and then i'm going to use json dot and then this is going to be dump s again because we want a string not a file then i'm going to put here the dictionary that i want to dump which is data so now if i print out new json you see that we get our json data printed out like this great now one other thing that we can do here when we are dumping our json data into a string is we can pass the argument indent now this will actually give us a nicely formatted json string with the proper indentation so in this case if i go indent two that's the indentation level it's going to give us so let's actually print this out now and notice we get a nicely formatted json string now if i want a higher indentation i can do four and of course now i get kind of the tab indentation that we usually use now another thing that i can do is pass something called sort keys pretty intuitive this is going to sort the keys in our json data so if i do this now notice we have age full time id and name whereas in here we had id name age full time great that's as easy as it is to work with json strings now i want to show you how to work with json files all right so i've just created a json file here in the same directory as my python script it just has the same data i had before in that string but i've called it data dot json now one important difference here with json and python is when you're doing the booleans you're going to have lowercase so i have true with a lowercase t non-uppercase just figured i would mention that okay so that is my data i'm now in my python script let's say i want to load in this data file and get access to it as a python dictionary how would i do that well it's very simple but i first need to open the file so i'm going to say with open i'm going to put my file name which is data.json i'm going to open this in read mode i'm going to open this as f then what i can do is the following i can say my data is equal to json json.load now notice how i'm not doing the s this time because i'm loading from a file and i'm going to put the file object that i just opened which in this case is f now what i can do here is simply print the data and when i do this i get my data again as a python dictionary to prove this to you we can print out the items and you'll see that we get our dict items all right there we go that is how you load json data from a file now let's see how we can write json data to a file so again we have our data right here if i want to write to a file i need to open a file in write mode and then use json.dumps so i'm just going to use the same dictionary that i have right here so i'm going to say with open this is going to be data2.json we're going to open this in write mode because we're creating a new file i'm going to say as f and now i'm going to say json dot dump notice no s right just dump then i'm going to put the data that i want to dump which in this case is data and the file i want to dump it to which is f okay let's go ahead and run this and now if i go to data2.json you're going to see that i have my json data in here now it's not formatted nicely but that's fine you usually don't care about the formatting when you're dumping it to a font all right so that has been json in python as fast as possible there's a few other things you can do with this however this is really all you need to know to be able to load data save data load data from a file and save data to a file i hope this was helpful to you if it was make sure to subscribe leave a like on the video check out programming expert if you want to become a software engineer as fast as possible link in the description you can use discount code tim and i hope to see you in another youtube video [Music] you
Original Description
How fast do you want to learn JSON in Python? Well if your answer is really fast then you're in the right spot! In this video I am going to show you how to work with JSON data in Python as fast as possible! I will go into what JSON actually is, how to load and create JSON strings as well as how to read and write JSON files! I hope you enjoy!
💻 ProgrammingExpert is the best platform to learn how to code and become a software engineer as fast as possible! Check it out here: https://programmingexpert.io/tim and use code "tim" for a discount!
⭐️ Timestamps ⭐️
00:00 | JSON Defined
01:15 | Loading JSON Strings
02:20 | Creating JSON Strings
03:46 | Reading JSON Files
04:51 | Writing JSON To A File
05:36 | Outro
◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
👕 Merchandise: https://teespring.com/stores/tech-with-tim-merch-shop
📸 Instagram: https://www.instagram.com/tech_with_tim
📱 Twitter: https://twitter.com/TechWithTimm
⭐ Discord: https://discord.gg/twt
📝 LinkedIn: https://www.linkedin.com/in/tim-ruscica-82631b179/
🌎 Website: https://techwithtim.net
📂 GitHub: https://github.com/techwithtim
🔊 Podcast: https://anchor.fm/tech-with-tim
🎬 My YouTube Gear: https://www.techwithtim.net/gear/
💵 One-Time Donations: https://www.paypal.com/donate?hosted_button_id=CU9FV329ADNT8
💰 Patreon: https://www.patreon.com/techwithtim
◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
⭐️ Tags ⭐️
-Tech With Tim
-How to Use JSON
-JSON In Python
-Use JSON In Python
-JSON and Python
⭐️ Hashtags ⭐️
#TechWithTim #JSON #Python
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
Related Reads
📰
📰
📰
📰
Token Efficiency for AI Coding Agents: A Practical Guide
Dev.to · Aleksandr Kamenev
AI Can Write Code. So What Makes a Developer Valuable? Why PyNyx Thinks the Answer Has Changed
Dev.to · PyNyx
Claude Code From My Phone Is an Unfair Advantage.
Medium · AI
Your Codebase Just Wrote Itself. Terrified Yet?
Dev.to · Chathura Rathnayaka
Chapters (6)
| JSON Defined
1:15
| Loading JSON Strings
2:20
| Creating JSON Strings
3:46
| Reading JSON Files
4:51
| Writing JSON To A File
5:36
| Outro
🎓
Tutor Explanation
DeepCamp AI