
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to generate JSON output using Python?
The json module in python allows you to dump a dict to json format directly. To use it,
Example
import json my_dict = { 'foo': 42, 'bar': { 'baz': "Hello", 'poo': 124.2 } } my_json = json.dumps(my_dict) print(my_json)
Output
This will give the output −
'{"foo": 42, "bar": {"baz": "Hello", "poo": 124.2}}'
You can also pass indent argument to prettyprint the json.
example
import json my_dict = { 'foo': 42, 'bar': { 'baz': "Hello", 'poo': 124.2 } } my_json = json.dumps(my_dict, indent=2) print(my_json)
Output
This will give the output −
{ "foo": 42, "bar": { "baz": "Hello", "poo": 124.2 } }
- Related Articles
- How to generate XML using Python?
- How to parse JSON input using Python?
- How to selectively retrieve value from json output JavaScript
- How to convert string to JSON using Python?
- How to generate prime numbers using Python?
- How to generate prime twins using Python?
- How to generate statistical graphs using Python?
- How to generate a 24bit hash using Python?
- How to generate pyramid of numbers using Python?
- How to serialize a JSON string to an Output Handler in Java?
- How to generate secure temporary file name using Python?
- How to generate a string of bits using Python?
- How to generate JSON data and perform Schema Validation in Oracle?
- How to generate a random 128 bit strings using Python?
- How to generate random regression problems using Python Scikit-learn?

Advertisements