GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — Python JSON: Convert Python object to JSON data - w3resource

Python JSON: Convert Python object to JSON data

Last update on June 28 2025 12:50:21 (UTC/GMT +8 hours)


2. Convert Python Object to JSON Data

Write a Python program to convert Python object to JSON data.

Sample Solution:-

Python Code:

import json
# a Python object (dict):
python_obj = {
  "name": "David",
  "class":"I",
  "age": 6  
}
print(type(python_obj))
# convert into JSON:
j_data = json.dumps(python_obj)

# result is a JSON string:
print(j_data)

Output:

<class 'dict'>
{"name": "David", "class": "I", "age": 6}
 

Flowchart:

Flowchart: Convert Python object to JSON data.


For more Practice: Solve these Related Problems:

  • Write a Python program to serialize a Python object containing nested dictionaries and lists into a JSON string with sorted keys.
  • Write a Python program to convert a Python object with non-ASCII characters into a JSON string and ensure proper encoding.
  • Write a Python program to implement a custom JSON encoder that handles datetime objects when converting a Python dictionary to JSON.
  • Write a Python program to convert a Python object into a formatted JSON string and output it to the console with a specified indentation level.

Go to:

  • Python JSON Exercises Home ↩
  • Python Exercises Home ↩

Previous: Write a Python program to convert JSON data to Python object.
Next: Write a Python program to convert Python objects into JSON strings. Print all the values.

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.