JSON Strings and Python Objects for Data Wrangling: A Beginner's Guide (Part 1)
Ever come across a weird-looking bunch of curly braces and quotation marks holding all sorts of information? That, my friends is JSON (JavaScript Object Notation), a super common way to store and share data these days. It's kind of like a secret code for computers to talk to each other, but worry not, we're about to crack that code together. Whether you're a Python newbie or just getting familiar with data wrangling, this tutorial is for you. We'll be venturing into the exciting world of JSON, and by the end, you'll be a JSON wrangler extraordinaire!
Here's what we'll be conquering today:
So, grab your favorite coding hat (or cat, no judgment here ) and let's get started!
Unveiling the Mystery: What's the Deal with JSON?
Imagine you have a secret message for your friend, but instead of weird symbols, you use notes with words and phrases. That's kind of what JSON (JavaScript Object Notation) is like! It's a way for computers to exchange information in a format that's easy for both humans and machines to understand.
JSON started out as a way for web browsers and servers to chat, but it's become a superstar for all sorts of data exchange. Here's why it's so awesome:
Let's see JSON in action! Imagine you're writing a blog post and want to store some information about it. Here's what that might look like in JSON:
Pretty cool, right? We can see the title, author, content, and even some tags, all neatly organized in this JSON code. That's the power of JSON – clear, compact, and ready to use!
JSON Ground Rules: Building Your Data Castle
Before we jump into wrangling JSON data with Python, let's establish some ground rules, like building blocks for your JSON castle! These rules will ensure your data is structured correctly and understood by both humans and computers.
Here's an example showcasing arrays and nested objects:
In this example, we have an object with an "author" key containing another object with nested "name" and "experience" information. Additionally, the "topics" key holds an array with strings and a nested object containing a "subtopic" with its own array of "frameworks."
The table below shows how conversion to and from Json will be treated and I will leave that for you to verify and have a taste of being part of the designing of this tutorial. Meet me in the next section once you are done quenching your curiosity.
The weapon of our warfare is not carnal but the json Module!
Now that we've cracked the JSON code and understood its structure, it's time to bring that data into python! Buckle up, because we're about to perform some data wrangling magics. Python provides a built-in tool, the json module specifically designed for working with JSON data. It's like having a decoder ring that lets you translate between the JSON code and python objects. To use this magic tool, we first need to import it into our python code
In the context of the json module, these are two important functions:
These two functions are essential for working with JSON data in Python. They allow you to convert between Python objects and JSON strings, which makes it easy to store, transmit, and work with data in a format that is widely used by many different applications and systems.
Here is an example of how to use the dumps() and loads() functions:
Recommended by LinkedIn
In this example:
Things are about to get even more interesting!
I have a snippet for us to look into.
Looks like we’ve run out of luck here, our output is a disappointment.
Our object has been created but things went bad on line 20 in our attempt to serialize post. A TypeError is raised and the message says post is not JSON serializable. In all honesty my friends, you cannot dump the content of an object even if it is as simple as this example. What’s the hack? Well, we can alternatively dump post.__dict__ but loading it will return a dict. The snippet below says it clearly!
Lemme show you the way out, we’re gonna redirect “default()” - the function dumps() uses to obtain a textual representation of its argument and “object_hook()” - the function loads() uses to convert json elements to python object, have a look!
All I have done is to write my own functions that understand our object and point default and object_hook to them. There is yet another way of achieving this same result that involves writing our own custom classes that inherit from json.JSONDecoder and json.JSONEncoder and twigging them to achieve our desired output. Since this post is becoming a little longer, I will be leaving out that part.
Conclusion:
We've conquered the basics of transforming JSON strings into Python objects and vice versa using the built-in json module! Now you have the power to unlock and manipulate data stored in JSON format.
But what if you encounter data that's not quite JSON-friendly? Fear not, Python warriors! In the next part of this series, we'll delve into the world of custom JSON encoders and decoders classes, handling exceptions that might arise in the process, tackling more demanding examples - empowering you to handle even the trickiest data types.
Did you find this post helpful? Let me know in the comments below!
Do you have any questions about working with JSON data in Python?
Share this post with your fellow Python enthusiasts who might be curious to crack the JSON code!
Stay tuned for the next part, where we'll conquer custom JSON encoder and decoders classes together!
Transforming Businesses and Lives Through Creative Writing, Media, Strategic Digital Marketing, Talent Acquisition, Customer Management, and AI.
7moGreat tutorial! You broke down JSON and its integration with Python in a fun and approachable way.
Student at ALX Africa
9moWow. Well done.
Hire me! & save 30+ hours weekly | I will clear 100% of routine tasks in 6 hours & improve your daily FOCUS! | Upwork Top Rated CEO Executive Assistant | Phone, Email & Chats Assistance
9moUseful tips here MICHAEL OKO 🔥🔥⭐
Data Scientist | GPSDD CAN Fellow
9moScholar Michael!! Great insightful post👍🏾
Senior Software Engineer | Technical Support Engineer | Data Scientist
9moAmazing. This is really handy, I will bookmark this.