JSON formatting in Python



Javascript Object Notation, or JSON for short, is a lightweight data interchange format. It encodes Python objects as JSON strings and decodes JSON strings into Python objects.

  • Many of the APIs like Github . send your results in this format. JSON is probably the most widely used for communication between a web server and a client in an AJAX application, but is not limited to this problem area.
  • For example, if you are trying to create an exciting project like this , we need to format the JSON output to get the results we want. So let`s dive into the json module that Python offers for formatting JSON output.

functions

  • json.dump (obj, fileObj) : Serializes as a JSON stream for.
  • json.dumps (obj) : Serializes as a JSON string .
  • json.load (JSONfile) : deserializes to a Python object.
  • json.loads (JSONfile) : deserializes (type: string) into a Python object.
  • Classes

  • JSONEncoder: encoder class for converting Python objects to JSON.
  • JSONDecoder: decoder class for converting JSON file to Python obj.
  • Conversions are based on this conversion table .


    Implementation

    encoding
    We will use dump (), dumps () and the JSON.Encoder class.

    # The code will work 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" +

    str (TypeEncoder (). encode ( type ( list ))))

    print ( "Using json.JSONEncoder (). iterencode" +

    str ( list (TypeEncoder (). iterencode ( type ( dict )))))

    Output:

     Using json.dump (): ["Hello", "Geeks"] Using json.dumps ( ): "" Using json.JSONEncoder (). Encode "" Using json.JSONEncoder (). Iterencode [`" "`] 

    Decoding
    We will use load (), load () and the JSON.Decoder class.

    # Code will work 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}` )))

    Output:

     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) 

    Link :

    This article is courtesy of  Sri Sanket Uppalapati . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

    Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.