- MCP server with stdio transport for local use - Search episodes, transcripts, hosts, and series - 4,511 episodes with metadata and transcripts - Data loader with in-memory JSON storage 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
132 lines
11 KiB
Plaintext
132 lines
11 KiB
Plaintext
Episode: 2010
|
|
Title: HPR2010: Parsing JSON with Python
|
|
Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr2010/hpr2010.mp3
|
|
Transcribed: 2025-10-18 13:16:27
|
|
|
|
---
|
|
|
|
This is HPR Episode 2010 entitled, Passing Case on WinPython, and in part on the series,
|
|
a little bit on Python, it is hosted by Klaatu, and in about 12 minutes long, the summary is,
|
|
how to pass Case on WinPython.
|
|
This episode of HPR is brought to you by an Honesthost.com.
|
|
Get 15% discount on all shared hosting with the offer code, HPR15, that's HPR15.
|
|
Get your web hosting that's Honest and Fair, at An Honesthost.com.
|
|
Hi everyone, my name is Klaatu, and this is Hacker Public Radio.
|
|
Today I want to talk about using JSON in Python, so JSON, of course, is a pretty popular way
|
|
of storing data these days.
|
|
It's essentially a key in value type arrangement, where you've got a key value of some sort,
|
|
and then a colon, and then a value, and it can get more complex because they can be nested,
|
|
you know, you can have a key value pair in the value of another key value pair.
|
|
If you've never seen it, look it up online.
|
|
I wanted to say you've probably seen it and haven't realized it, but maybe not actually,
|
|
you may not have ever seen it, but yeah, it's a pretty popular way to store data.
|
|
It's sort of from out of the JavaScript school of data management, JSON, in fact,
|
|
stands for JavaScript, something, something.
|
|
So yeah, it's popular, and it's good, and people like it, and it keeps popping up.
|
|
You know, you'll kind of see JSON and JSON-like things here and there.
|
|
And it is kind of a simple way of storing things, you know.
|
|
If you have some kind of data that you need to store, a lot of times, essentially,
|
|
what you are doing is just storing a key in the value, you know.
|
|
This thing, this configuration option, or this environment variable, or this player in this game,
|
|
has the value of, has a score of, or has health points of, or has a configuration of,
|
|
and then whatever that is. So it's kind of like, I kind of feel like it's one of the most basic
|
|
possible ways of structuring this kind of information, which is quite nice.
|
|
If you look at a JSON file, and like I say, if you can't picture it in your head,
|
|
just go look at it online. If you look at it, you'll see that it actually looks a little bit like,
|
|
for instance, a Python dictionary. The two are actually quite, quite similar.
|
|
If you're comfortable with Python dictionaries and lists,
|
|
chances are, if you use JSON in Python, it actually will feel fine. It'll feel perfectly normal to you.
|
|
You may or may not be comfortable with dictionaries in Python. If you don't use them a lot,
|
|
if you're kind of a new user of Python, and you're not creating lists and dictionaries,
|
|
and I guess words yet, dictionaries of lists and lists of dictionaries and things like that,
|
|
then you might not feel all that comfortable with JSON, and you might wonder, well, can't I just
|
|
use a simple key in value, a line-delimited file? And the answer to that would be, yeah, you can
|
|
do that. You can structure your data however you want, to be honest. You do kind of have to step
|
|
back and think, if I do this my way, just a hand-rolled delimited list of something,
|
|
is that the best way to do it? And it may be. It really just depends on what data you're trying to store.
|
|
So, in other words, you don't necessarily have to use anyone's format at all. You can just,
|
|
I've done this before myself. You just make a file and you print some statements into it that
|
|
that, you know, mirror your user's input, and then you parse that file later on when they
|
|
load the application back up, and it's not that hard, you use split, and read in the data.
|
|
It's not really that big of a deal, but JSON does like a lot of that for you.
|
|
Okay, so the way to do this, I guess, in a very basic, very basic example would be, in fact,
|
|
to create a dictionary in Python, and essentially, in many ways, when you're doing that,
|
|
you are creating a JSON structured entity. So, let's say that we have a dictionary we want to
|
|
call game, because this is going to keep score of a game for us. So, if we say game equals
|
|
curly brace, quote, tux, quote, colon, and then it's not that hard. It's a very, it's a very sort of
|
|
built-in method for Python. That's all very standard stuff. You're going to do that.
|
|
At some point, if you use Python, you're going to be dealing with a dictionary. So, you may as well
|
|
just kind of start doing it now. So, the cool thing about that is, though, that if you're storing
|
|
your data in a Python dictionary like that, it's, it is trivial to dump it into a JSON format
|
|
file. I mean, really trivial. So, what you would do is you would just, in Python, you would import
|
|
JSON. And then with open parentheses, quote, save data dot JSON, close quote, comma, quote,
|
|
w, quote, close parentheses, as out file colon, and then next line intended once JSON dot dump
|
|
parentheses, game, comma, out file, close parentheses. So, what we're doing there is we're saying,
|
|
open this file called game save, or save data, whatever I called it, save, save data dot JSON,
|
|
open it with writable permissions, and we'll refer to it as out file. That's the variable name,
|
|
we'll assign to this thing. And then with that open in write mode, give me a JSON dump, a JSON
|
|
formatted dump of the data contained in the game variable, and put that into out file. And that's
|
|
that's how you save the data. So, if you were writing, for instance, a game, or I don't know,
|
|
a note taking application, or, you know, whatever, or a configuration file, if you've been storing
|
|
all the active data while a person's running your program into a Python dictionary, and why
|
|
wouldn't you be? It's a very sensible format to store it in. Then when they exit your application,
|
|
before they exit, you know, as they exit your application as part of the quit process,
|
|
you could open up a configuration file in some given path of your choosing,
|
|
and dump the Python dictionary that they've been building as they've run the application
|
|
to that place, and now it will be persistent data. So, what do you do when you, when the user
|
|
then opens up the file or the application again? Well, you want to probably read in their previous
|
|
session, right? That's what config files do, or maybe there, maybe it's not a config file,
|
|
maybe it's save data. So, then if they're playing your game, and they say, well, I'll play as
|
|
tux today, then you would want to load in the last known values for tux, right? So, again,
|
|
the process, it's really, it's basically the same as reading a dictionary, but the only thing
|
|
that you have to do is load the data in using that JSON module. So, you can do import JSON,
|
|
and then you could say, let's say, file, game, what does it say, save data equals, open parentheses,
|
|
quote, save data.json quote parentheses. So, that would be the file, the save file, and then we
|
|
could assign a variable game equals json.load parentheses, save data, close parentheses. So, in other
|
|
words, instead of doing a JSON dump to the save data file, we're now opening the save data file,
|
|
because we know that it exists, or we assume it exists. And in real life, you would want to do
|
|
probably like a try, except kind of statement, because you wouldn't obviously want to crash just
|
|
because the person's never saved a game before. But anyway, we're opening that file, and we're
|
|
loading it into the variable called game. And because we do it through this JSON module, it
|
|
parses everything from JSON, and dumps it into a nice little tidy Python dictionary. And you
|
|
can even see that. So, if you, if you call the game module again, or the game variable rather,
|
|
just game, square bracket, quote, tux, quote, square bracket, then it would print for you,
|
|
curly brace, health 23, level four. And if you did game, square bracket, quote, conky,
|
|
quote, square bracket, then it would print, uh, health 18, level seven. And you can even drill
|
|
down further into it. So, you know, you don't always have to get all the information back. You could
|
|
say, well, okay, so I, right now, I know that it's the, they've chosen the tux character,
|
|
but I need to know just the health values that I can, or, or just the level values that I can
|
|
dump them into the right level. So you could do a game, square bracket, quote, tux, quote, square
|
|
bracket, and then square bracket, quote, level, quote, square bracket. So that's just, you're just
|
|
giving it sort of both positions, tux, and then drill down into level and give me the value. And
|
|
that would print for, or you could do print, uh, or rather game, square bracket, tux, square,
|
|
bracket, square bracket, health square bracket, and that would print 23, where you could do game,
|
|
conky, health, and that would give you seven or whatever it was at 18. And then the level would
|
|
be seven. So you, you can just, it's, it's a dictionary. You can sort of target what you need to
|
|
know in square bracket with quotes around the, the key that you want to look up, even if it's a
|
|
key within the value of the previous key. Uh, eventually you'll want to close that file too. You
|
|
would want to save file, save data dot clothes, parentheses, parentheses. Once you've got all that
|
|
data and loaded it into your environment. And then of course at the end of that session,
|
|
you would want to reopen that file and, and dump your new, your new information into it.
|
|
It's pretty simple to be honest. There are other modules out there that do fancier things with
|
|
JSON data in Python. Um, I heard actually really recently that someone was really annoyed with how
|
|
Python works with JSON data. I've not really done a whole lot with JSON outside of Python. So I
|
|
don't know what I'm missing. Um, I find it very easy to be honest. So, uh, but then again,
|
|
all I do really is read and write. I don't do a whole lot of fancy things with it. So it, it,
|
|
it may, you know, it may get more complex, the more complex things that you try to do with it.
|
|
And it might start to kind of show some of its, its inconveniences. Um, but either way, you know,
|
|
if you're looking to do quick data storage from a Python application that you're working on,
|
|
JSON is not the worst way to go. It's, it's pretty simple. It feels very native, uh, since it's
|
|
so close to a Python dictionary anyway. Um, and yeah, you should try it out if you're,
|
|
if you're looking for, uh, solution for that. Thanks for listening to this episode of Hacker
|
|
Public Radio. Uh, this has been Clot 2 and I will see you next time.
|
|
You've been listening to Hacker Public Radio at Hacker Public Radio dot org.
|
|
We are a community podcast network that releases shows every weekday, Monday through Friday.
|
|
Today's show, like all our shows, was contributed by an HBR listener like yourself.
|
|
If you ever thought of recording a podcast and click on our contributing,
|
|
to find out how easy it really is. Hacker Public Radio was founded by the digital
|
|
dog pound and the infonomicon computer club, and it's part of the binary revolution at binrev.com.
|
|
If you have comments on today's show, please email the host directly, leave a comment on the website
|
|
or record a follow-up episode yourself. Unless otherwise stated, today's show is released on
|
|
creative comments, attribution, share a life, 3.0 life zones.
|