![]() |
VOOZH | about |
JSON is a lightweight data format for data interchange which can be easily read and written by humans, easily parsed and generated by machines. It is a complete language-independent text format. To work with JSON data, Python has a built-in package called json.
json.dumps() method can convert a Python object into a JSON string.
json.dumps(dict, indent)
Parameters:
Example:
{
"id": "04",
"name": "sunil",
"department": "HR"
}
| Python | JSON Equivalent |
|---|---|
| dict | object |
| list, tuple | array |
| str | string |
| int, float | number |
| True | true |
| False | false |
| None | null |
json.dump() method can be used for writing to JSON file.
json.dump(dict, file_pointer)
Parameters:
Example:
Output:👁 python-json-write-to-file
Note: For more information, refer to Working With JSON Data in Python
Let us see the differences in a tabular form -:
| json.dump() | json.dumps() |
|---|---|
| json.dump() method used to write Python serialized object as JSON formatted data into a file. | json.dumps() method is used to encodes any Python object into JSON formatted String. |
Its syntax is: json.dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw) | Its syntax is: json.dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw) |
| It is used to perform compact encoding to save file space | It takes 7 parameters. |
| It is used to skip nonbasic types while JSON encoding | It can be used with Lists. |