Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JSON Formatting in Python

  • Difficulty Level : Hard
  • Last Updated : 06 Jun, 2018

Javascript Object Notation abbreviated as JSON is a light-weight data interchange format. It Encode Python objects as JSON strings, and decode JSON strings into Python objects .

  • Many of the APIs like Github. send their results in this format. JSON is probably most widely used for communicating between the web server and client in an AJAX application, but is not limited to that problem domain.
  • For example, if you are trying to build an exciting project like this, we need to format the JSON output to render necessary results. So lets dive into json module which Python offers for formatting JSON output.


  • json.dump(obj, fileObj): Serializes obj as a JSON formatted stream to fileObj.
  • json.dumps(obj) : Serializes obj as JSON formatted string.
  • json.load(JSONfile) : De-serializes JSONfile to a Python object.
  • json.loads(JSONfile) : De-serializes JSONfile(type: string) to a Python object.


  • JSONEncoder: An encoder class to convert Python objects to JSON format.
  • JSONDecoder: A decoder class to convert JSON format file into Python obj.

    The conversions are based on this conversion table.


    We will be using dump(), dumps() and JSON.Encoder class.

    #Code will run in Python 3
    from io import StringIO
    import json
    fileObj = StringIO()
    json.dump(["Hello", "Geeks"], fileObj)
    print("Using json.dump(): "+str(fileObj.getvalue()))
    class TypeEncoder(json.JSONEncoder):
        def default(self, obj):
            if isinstance(obj, type):
                return str(obj)
    print("Using json.dumps(): "+str(json.dumps(type(str), cls=TypeEncoder)))
    print("Using json.JSONEncoder().encode"+
    print("Using json.JSONEncoder().iterencode"+


    Using json.dump(): ["Hello", "Geeks"]
    Using json.dumps(): ""
    Using json.JSONEncoder().encode""
    Using json.JSONEncoder().iterencode['""']

    We will be using load(), loads() and JSON.Decoder class.

    #Code will run in Python 3
    from io import StringIO
    import json
    fileObj = StringIO('["Geeks for Geeks"]')
    print("Using json.load(): "+str(json.load(fileObj)))
    print("Using json.loads(): "+str(json.loads('{"Geeks": 1, "for": 2, "Geeks": 3}')))
    print("Using json.JSONDecoder().decode(): " +
        str(json.JSONDecoder().decode('{"Geeks": 1, "for": 2, "Geeks": 3}')))
    print("Using json.JSONDecoder().raw_decode(): " +
        str(json.JSONDecoder().raw_decode('{"Geeks": 1, "for": 2, "Geeks": 3}')))


    Using json.load(): ['Geeks for Geeks']
    Using json.loads(): {'for': 2, 'Geeks': 3}
    Using json.JSONDecoder().decode(): {'for': 2, 'Geeks': 3}
    Using json.JSONDecoder().raw_decode(): ({'for': 2, 'Geeks': 3}, 34)


    This article is contributed by Sri Sanketh Uppalapati. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

    My Personal Notes arrow_drop_up
  • Recommended Articles
    Page :

    Start Your Coding Journey Now!