![]() |
VOOZH | about |
We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.
Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.
Follow TNS on your favorite social media networks.
Become a TNS follower on LinkedIn.
Check out the latest featured and trending stories while you wait for your first TNS newsletter.
JavaScript Object Notation (JSON) facilitates information sharing between applications. It’s the go-to choice for exchanging data between web clients and servers and for communication between APIs and various services or data sources. JSON’s format is both machine-readable and human-friendly, making it easy to parse, generate, and understand. Its efficiency in reducing data transmission makes JSON the go-to format for information sharing
JSON is:
Working with JSON data is a common occurrence when building and maintaining applications.
JSON performs the following tasks quickly and efficiently.
Importing the `json` module is the first step.
Parsing JSON data converts JSON-encoded data (usually in string form) into a format that can be accessed within a programming environment. In Python applications, the JSON string is converted into native Python data structures like dictionaries and lists.
`json.loads()` is the `json` module’s data parsing function.
Output: {‘apple’: ‘red’, ‘banana’: ‘yellow’, ‘cucumber’: ‘green’, ‘types’: [‘fruit’, ‘vegetable’]}
The `json.loads()` function is the same for both nested and unnested data.
You can access the JSON data after parsing.
Hardcoded:
Output:
red
yellow
[‘fruit’, ‘vegetable’]
Dynamic:
39
Often, JSON data is nested. This means the data includes dictionaries or lists within other dictionaries or lists.
Nested data is incredibly common because it mirrors how data is organized in the real world. An example of nested data is a user profile on an e-commerce site that contains personal details, addresses, and a list of purchases, all with their own attributes.
Accessing nested JSON data requires a different code setup than unnested data.
Hardcoded:
Output:
Sarah Ellington
B+
Dynamic:
Working with nested data dynamically requires a recursive or iterative function. Though both work, recursive solutions are more elegant and slightly easier to read.
Output:
`json.dumps()` saves JSON data in a file. json.dumps() converts Python objects into a JSON-formatted string, known as serializing the data. The serialized data is then written to a file or transmitted over a network. Writing nested and unnested data follows similar protocols.
Unnested:
Nested:
open('data.json', 'w') opens data.json in write mode. Write mode overwrites the file if it exists or creates a new file if it doesn’t.
Reading data differs between nested and unnested data. Since nested data is so common when in doubt, write a function that reads nested data. The json module’s data reading function is json.load(file).
Here’s the unnested version just for reference and comparison.
Pretty printing JSON data improves readability and simplifies debugging by formatting the data with indentation and line breaks. Pretty printing provides a clear, accessible format for shared data. This helps with documentation and collaboration.
The indent parameter in the json.dumps() function formats JSON data with the indentation. Pretty printing follows the same process for both nested and unnested data.
The number following `indent` in the `json.dumps()` function dictates how many spaces to indent. Four is fairly common though indent best practices can vary by system.
Output:
JSON presents different exceptions depending on how you interact with the data, but in all cases, using try…except blocks is the standard approach for handling errors when working with JSON.
Output:
Failed to decode JSON: Expecting property name enclosed in double quotes: line 1 column 29 (char 28)
For more on how to handle errors, check out our error-handling tutorial.
JSON is a common format for data transmitted through APIs, and the `requests` library is frequently used to handle such data in Python.
Here’s an example of how to retrieve JSON data from an API with error handling:
JavaScript Object Notation (JSON) is a fundamental tool for data interchange between applications. Its efficiency in reducing data transmission makes it the preferred choice for web clients, servers and API interactions. Understanding JSON’s capabilities and best practices, including proper error handling and pretty printing, are the first steps toward working efficiently in real-world scenarios.