View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
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.
Note: For more information, refer to Working With JSON Data in Python
json.dumps[]
json.dumps[] method can convert a Python object into a JSON string.
Syntax: json.dumps[dict, indent]
Parameters:
- dictionary – name of dictionary which should be converted to JSON object.
- indent – defines the number of units for indentation
Example:
Python3
import
json
dictionary
=
{
"id"
:
"04"
,
"name"
:
"sunil"
,
"department"
:
"HR"
}
json_object
=
json.dumps[dictionary, indent
=
4
]
print
[json_object]
Output:
{ "department": "HR", "id": "04", "name": "sunil" }
Python objects and their equivalent conversion to JSON:
dict | object |
list, tuple | array |
str | string |
int, float | number |
True | true |
False | false |
None | null |
json.dump[]
json.dump[] method can be used for writing to JSON file.
Syntax: json.dump[dict, file_pointer]
Parameters:
- dictionary – name of dictionary which should be converted to JSON object.
- file pointer – pointer of the file opened in write or append mode.
Example:
Python3
import
json
dictionary
=
{
"name"
:
"sathiyajith"
,
"rollno"
:
56
,
"cgpa"
:
8.6
,
"phonenumber"
:
"9976770500"
}
with
open
[
"sample.json"
,
"w"
] as outfile:
json.dump[dictionary, outfile]
Output:
Let us see the differences in a tabular form -:
json.dump[] | json.dumps[] | |
1. | 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. |
2. | 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] |
3. | It is used to perform compact encoding to save file space | It takes 7 parameters. |
4. | It is used to skip nonbasic types while JSON encoding | It can be used with Lists. |
One notable difference in Python 2 is that if you're using ensure_ascii=False
, dump
will properly write UTF-8 encoded data into the file [unless you used 8-bit strings with extended characters that are not UTF-8]:
dumps
on the other hand, with ensure_ascii=False
can produce a str
or unicode
just depending on what types you used for strings:
Serialize obj to a JSON formatted str using this conversion table. If ensure_ascii is False, the result may contain non-ASCII characters and the return value may be a
unicode
instance.
[emphasis mine]. Note that it may still be a str
instance as well.
Thus you cannot use its return value to save the structure into file without checking which format was returned and possibly playing with unicode.encode
.
This of course is not valid concern in Python 3 any more, since there is no more this 8-bit/Unicode confusion.
As for load
vs
loads
, load
considers the whole file to be one JSON document, so you cannot use it to read multiple newline limited JSON documents from a single file.