When working with JSON (JavaScript Object Notation) in Python, it's often necessary to save the formatted JSON data for later use or to share between different parts of a project. Python provides several ways to achieve this, using both built-in functionalities and external libraries.

Before diving into the various methods, let's first consider a simple JSON structure that we'll use for our examples. Consider a dictionary named data:

```python data = { "name": "John", "age": 30, "city": "New York" } ```
Using Built-in `json` Module
The json module in Python offers functionalities to parse, manipulate, and stringify JSON data. It is the preferred method for simplicity, efficiency, and built-in optimizations.

To save JSON data in a Python script, you can use the dumps() method to convert the Python object into a JSON formatted string, and then write it to a file.
Using `dumps()` Method

The dumps() method converts Python data structures into JSON strings. For instance, to dump the data dictionary as formatted JSON:
```python import json formatted_json = json.dumps(data, indent=4) ```
In the above code, indent=4 specifies the number of units for indentation. This parameter is especially useful for saving formatted JSON, as it creates readable and well-organized output.
Writing to a File

After obtaining the formatted JSON string, you can write it to a file using Python's file handling capabilities. Here's how to write the formatted_json string to a file named data.json:
```python with open('data.json', 'w') as json_file: json_file.write(formatted_json) ```
Using External Libraries - PrettyJSON
While the json module in Python offers powerful functionalities for JSON manipulation, external libraries such as prettyjson provide additional features for enhanced readability. The prettyjson library allows you to format JSON data as a tabular structure, similar to a YAML file.

After installing the library, you can use it to pretty-print and write JSON data to a file. Here's an example:
```python import json import prettyjson with open('data.json', 'w') as json_file: json_file.write(prettyjson.render_json(data)) ```
Formatting JSON in Jupyter Notebooks



















When working in a Jupyter Notebook, you might want to display JSON data in a formatted way. The json_formatter extension offers a convenient way to pretty-print JSON data in Jupyter Notebooks.
To enable JSON formatting in Jupyter Notebook, install the jupyter-contrib-nbextensions package and enable the json_formatter extension. Once enabled, you can format JSON data by wrapping it in $$ TRICKY: $$ tags, like this:
```python $$ JSON(data) $$ #{JSON(data)} ```
The formatted JSON will be displayed as the output of the cell. This method is particularly useful when exploring and visualizing JSON data in a Jupyter Notebook environment.
In conclusion, Python provides several methods for saving JSON data in a formatted way. From using the built-in json module to employing external libraries like prettyjson or formatting JSON in Jupyter Notebooks, each technique offers unique advantages depending on your specific requirements and usage context. Experimenting with these methods will help you determine the best approach for your projects.