Wednesday, December 25, 2024
Google search engine
HomeLanguagesjson.dumps() in Python

json.dumps() in Python

JSON is an acronym that stands for JavaScript Object Notation. Despite its name, JSON is a language agnostic format that is most commonly used to transmit data between systems, and on occasion, store data. Programs written in Python, as well as many other programming languages, can ingest JSON formatted data, and can serialize data in memory into the JSON format. Python supports JSON through a built-in package called json. To use this feature, import the json package into the Python script or module in which you wish to serialize or deserialize your data. JSON utilizes comma delimited key value pairs contained in double quotes and separated by colons. The body of a JSON file can be delimited in curly braces { } or square braces [] (also known as “brackets” in some locales). The JSON format appears to be similar to the dictionary in Python, but the specifics of the JSON format have significant differences, so use care when working with both formats.

Note: For more information, refer to Read, Write and Parse JSON using Python

Json.dumps()

json.dumps() function will convert a subset of Python objects into a json string. Not all objects are convertible and you may need to create a dictionary of data you wish to expose before serializing to JSON.
 

Syntax: 
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)
Parameters: 
obj: Serialize obj as a JSON formatted stream 
skipkeys: If skipkeys is True (default: False), then dict keys that are not of a basic type (str, int, float, bool, None) will be skipped instead of raising a TypeError. 
ensure_ascii: If ensure_ascii is True (the default), the output is guaranteed to have all incoming non-ASCII characters escaped. If ensure_ascii is False, these characters will be output as-is. 
check_circular: If check_circular is False (default: True), then the circular reference check for container types will be skipped and a circular reference will result in an OverflowError (or worse). 
allow_nan: If allow_nan is False (default: True), then it will be a ValueError to serialize out of range float values (nan, inf, -inf) in strict compliance of the JSON specification. If allow_nan is True, their JavaScript equivalents (NaN, Infinity, -Infinity) will be used. 
indent: If indent is a non-negative integer or string, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0, negative, or “” will only insert newlines. None (the default) selects the most compact representation. Using a positive integer indent indents that many spaces per level. If indent is a string (such as “\t”), that string is used to indent each level. 
separators: If specified, separators should be an (item_separator, key_separator) tuple. The default is (‘, ‘, ‘: ‘) if indent is None and (‘, ‘, ‘: ‘) otherwise. To get the most compact JSON representation, you should specify (‘, ‘, ‘:’) to eliminate whitespace. 
default: If specified, default should be a function that gets called for objects that can’t otherwise be serialized. It should return a JSON encodable version of the object or raise a TypeError. If not specified, TypeError is raised. 
sort_keys: If sort_keys is True (default: False), then the output of dictionaries will be sorted by key.

Example #1: Passing the Python dictionary to json.dumps() function will return a string. 
 

Python3




import json
 
# Creating a dictionary
Dictionary ={1:'Welcome', 2:'to',
            3:'Geeks', 4:'for',
            5:'Geeks'}
  
# Converts input dictionary into
# string and stores it in json_string
json_string = json.dumps(Dictionary)
print('Equivalent json string of input dictionary:',
      json_string)
print("        ")
 
# Checking type of object
# returned by json.dumps
print(type(json_string))


Output
 

Equivalent json string of dictionary: {“1”: “Welcome”, “2”: “to”, “3”: “Geeks”, “4”: “for”, “5”: “Geeks”} 
<class ‘str’>

Example #2: By setting the skipkeys to True(default: False) we automatically skip the keys that are not of basic type.
 

Python3




import json
 
 
Dictionary ={(1, 2, 3):'Welcome', 2:'to',
            3:'Geeks', 4:'for',
            5:'Geeks'}
 
 
# Our dictionary contains tuple
# as key, so it is automatically
# skipped If we have not set
# skipkeys = True then the code
# throws the error
json_string = json.dumps(Dictionary,
                         skipkeys = True)
 
print('Equivalent json string of dictionary:',
      json_string)


Output 
 

Equivalent json string of dictionary: {“2”: “to”, “3”: “Geeks”, “4”: “for”, “5”: “Geeks”} 
 

Example #3: 
 

Python3




import json
 
 
# We are adding nan values
# (out of range float values)
# in dictionary
Dictionary ={(1, 2, 3):'Welcome', 2:'to',
            3:'Geeks', 4:'for',
            5:'Geeks', 6:float('nan')}
 
# If we hadn't set allow_nan to
# true we would have got
# ValueError: Out of range float
# values are not JSON compliant
json_string = json.dumps(Dictionary,
                         skipkeys = True,
                         allow_nan = True)
 
print('Equivalent json string of dictionary:',
      json_string)


Output : 
 

Equivalent json string of dictionary: {“2”: “to”, “3”: “Geeks”, “4”: “for”, “5”: “Geeks”, “6”: NaN} 

Example #4: 
 

Python3




import json
 
 
Dictionary ={(1, 2, 3):'Welcome', 2:'to',
            3:'Geeks', 4:'for',
            5:'Geeks', 6:float('nan')}
 
# Indentation can be used
# for pretty-printing
json_string = json.dumps(Dictionary,
                         skipkeys = True,
                         allow_nan = True,
                         indent = 6)
 
print('Equivalent json string of dictionary:',
      json_string)


Output: 
 

Equivalent json string of dictionary: {
      "2": "to",
      "3": "Geeks",
      "4": "for",
      "5": "Geeks",
      "6": NaN
}

Example #5: 
 

Python3




import json
 
Dictionary ={(1, 2, 3):'Welcome', 2:'to',
            3:'Geeks', 4:'for',
            5:'Geeks', 6:float('nan')}
 
# If specified, separators should be
# an (item_separator, key_separator)tuple
# Items are separated by '.' and key,
# values are separated by '='
json_string = json.dumps(Dictionary,
                         skipkeys = True,
                         allow_nan = True,
                         indent = 6,
                         separators =(". ", " = "))
 
print('Equivalent json string of dictionary:',
      json_string)


Output: 
 

Equivalent json string of dictionary: {
      "2" = "to". 
      "3" = "Geeks". 
      "4" = "for". 
      "5" = "Geeks". 
      "6" = NaN
}

Example #6: 
 

Python3




import json
 
Dictionary ={'c':'Welcome', 'b':'to',
            'a':'Geeks'}
 
json_string = json.dumps(Dictionary,
                         indent = 6,
                         separators =(". ", " = "),
                         sort_keys = True)
 
print('Equivalent json string of dictionary:',
      json_string)


Output: 
 

Equivalent json string of dictionary: {
      "a" = "Geeks". 
      "b" = "to". 
      "c" = "Welcome"
}

 

RELATED ARTICLES

Most Popular

Recent Comments