---
title: "Python Objects to JSON String"
description: "Python provides a easy way to handle JSON, by importing thejson module, one can easily encode a list of dicts or a dict of."
url: "https://h3manth.com/scribe/python-objects-json-string/"
canonical_url: "https://h3manth.com/scribe/python-objects-json-string/"
markdown_url: "https://h3manth.com/scribe/blog/python-objects-json-string.md"
publishDate: "2010-12-29T00:00:00.000Z"
author: "Hemanth HM"
category: "python"
tags: ["python","javascript","bash"]
word_count: 191
reading_time_minutes: 1
x_markdown_tokens: 502
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# Python Objects to JSON String

> Python provides a easy way to handle JSON, by importing thejson module, one can easily encode a list of dicts or a dict of.

Python provides a easy way to handle JSON, by importing the**json** module, one can easily encode a list of dicts or a dict of dicts so on, but there is a easy trick to generate a JSON or even a nested JSON string from an object of a class.

```bash
json.dumps(obj[, skipkeys[, ensure_ascii[, check_circular[, allow_nan[, cls[, indent[, separators[, encoding[, default[, **kw]]]]]]]]]])`=> Serialize obj to a JSON formatted str
```

Consider an example, classic class called Student with few attributes like email,contact,name,skills,edu. Which are updated by other methods or at object creation.

` class Student:`

def **init**(self,name,email,contact,skills,ug=None,pg=None):
    
    
        self.email=email
        self.contact=contact
        self.name=name
        self.skills=[skills]
    
        self.edu={"ug":[ug],"pg":[pg]} 
    

Creating an instance of that class and prints the vars() we get something like :

` james=Student("James","j@j.com","","+1 7789990007","Python","CS", "CS")`

print vars(james) {
    
    
    'skills': ['Python'],
    'contact': '+1 7789990007',
    'email': 'J@j.com',
    'edu': {
        'ug': ['CS'],
        'pg': ['CS']
    },
    'name': 'James'
    

}

import json print json.dumps(vars(james),sort_keys=True, indent=4)

{
    
    
    "contact": "+1 7789990007", 
    "edu": {
        "pg": [
    
            "CS"
        ], 
        "ug": [
            "CS"
        ]
    
    }, 
    "email": "James@james.com", 
    "name": "James", 
    "skills": [
    
        "Python Ruby"
    ]
    

} 

P.S : This is just on the ways to Serialize obj to a JSON formatted str. Do share your ways below!
