How To Use JSON In Python

Tech With Tim · Beginner ·💻 AI-Assisted Coding ·4y ago

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 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This video teaches how to work with JSON data in Python, including loading and creating JSON strings and files, and using the json library to handle JSON data. It covers the basics of JSON and how to use it in Python programming.

Key Takeaways
  1. Import the json library
  2. Load JSON data from a string using json.loads()
  3. Dump JSON data to a string using json.dumps()
  4. Load JSON data from a file using json.load()
  5. Dump JSON data to a file using json.dump()
💡 The json library in Python provides an easy way to work with JSON data, including loading and creating JSON strings and files.

Related Reads

📰
Token Efficiency for AI Coding Agents: A Practical Guide
Optimize AI coding agent token spend with smart routing, planning, and culture shifts to improve efficiency
Dev.to · Aleksandr Kamenev
📰
AI Can Write Code. So What Makes a Developer Valuable? Why PyNyx Thinks the Answer Has Changed
AI can now write code, so what makes a developer valuable? Learn how the role of developers is changing with AI advancements
Dev.to · PyNyx
📰
Claude Code From My Phone Is an Unfair Advantage.
Use AI agents to accelerate coding tasks and gain an unfair advantage in development
Medium · AI
📰
Your Codebase Just Wrote Itself. Terrified Yet?
Explore the implications of AI-generated code on the software development industry and its potential impact on developer roles
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
Up next
Copilot Cowork: Setup, Skills, Plugins & Pricing
Matt Tutorials
Watch →